0

What is the best way to access methods of a java class?

  1. Creating object - if we create object every time, then It consume a lot of memory.
  2. if object creation is expensive then why do we not use static methods always for accessing resources of java class ?

Thanks in advance !!

krock
  • 28,904
  • 13
  • 79
  • 85
rawat0157
  • 13
  • 1
  • 1
  • 10
  • http://stackoverflow.com/questions/3903537/i-want-to-know-the-difference-between-static-method-and-non-static-method – underdog Apr 20 '15 at 06:29
  • 1
    possible duplicate of [Java: when to use static methods](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods) – Florian Apr 20 '15 at 06:32

5 Answers5

2

You want a behavior which should be constant for all the instances of the class use static, it's a good practice saves memory.

If you want the method behavior to change per instance of the class use a non-static method.

Say you have a program which builds a customer order.

A method which returns the standard packaging cost with any order should be static. A method which doesn't depend on the class properties should be static.

Whereas a method which returns the order cost depending on the chosen menu should be non-static.

Also too many static methods increase the class load time. Static methods cannot be overridden so you cannot leverage the power of polymorphism. Generally static methods are used as utility classes like Math.double() Math.float()

underdog
  • 4,447
  • 9
  • 44
  • 89
1

Objects are indeed more expensive, but it's often worth it for maintenance reasons: Your class encapsulates data which would be tedious to pass to a static method. E.g. if 'login' method needs to lookup user data from MySQL database & Mongo - then it's tedious to pass DB connections to each call:

   public class LoginService{
          public static login(String user,String password, DataSource mySQLConnections, MongoClient mongoClient) ... 

This signature is hard to read, and worse: it's not flexible to polymorphism. if we ever migrate user data from mySQL+mongo to LDAP, then we'll need to fix each call to "login(user,password, LDAPClient)"

Now, you may still argue this can be solved statically:

   public class LoginService{
          private static DataSource mySQLConnections;
          private static MongoClient mongoClient;
          public static login(String user,String password) ... 

   // and if we ever migrate to LDAP, we just change the internal statics, without affecting the public signature:
   public class LoginService{
          private static LDDAPClient;
          public static login(String user,String password) ... 

Sometimes that's just good enough, but many programmers still feel it's limiting. For details read about Spring and "Dependency Injection", but the bottom line is - I might want to play around with LoginService, have several instances (one that uses LDAP, another that uses MySQL+Mongo), and switch them based on some administration command, or even just for testing. This boils down to an interface with different implementing classes, each encapsulating its required helpers:

       public interface LoginService{
              public login(String user,String password) ... 
       }
       public class LoginServiceDb implements LoginService{
              private DataSource mySqlConnections;
              private MongoClient mongoClient;
              public login(String user,String password) 
                    // use the databases
              ...
       }
       public class LoginServiceLDAP implements LoginService{
              private LDAPClient ldap;
              public login(String user,String password) 
                // use LDAP
              ...  
}

Also note: effect on the garbage collection might not be that bad (depending on your needs an how much 'real time' you are). Some Objects might be cached and re-used. Some objects are used so briefly that they remain in the "infant" generation which the GC handles reasonably well

Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11
0

It all depends on whether or not the behaviour of the method should go for one instance of the class, or for every possible instance.

We don't always use static, because from time to time, we expect Objects to react differently. Let's say, we have a Person class, with two "instances", you and me. If we were to implement a method:

public static String getName(){ ....
}

one of us would have to change his name. For this, you expect a different reply for each instance of Person, so static is not appropriate here.

Here, you'll need two instances, an instance variable name (set by the constructor or mutator) which for me would return "Hans", and for you would return "Pushpendra"

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

A static method belongs to the class and a non-static method belongs to an object of a class. That is, a non-static method can only be called on an object of a class that it belongs to. A static method can however be called both on the class as well as an object of the class. A static method can access only static members. A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself). In the other case, a non-static method can only be called when the class has already been instantiated. A static method is shared by all instances of the class.

I have got data from here.

Community
  • 1
  • 1
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
0

Best way!!?
There is no any best way. All the ways are useful and good depending on your purpose.

The decision of making methods static on not don't only depend on the object creation and its expense You need to consider the usability too.

If a method can be shared for all instances and do not need to depend on any object property then you can happily make it static.

Its also need to consider how you going to use it. Remember a static method or field doesn't belong to objects, its belong to the class

Saif
  • 6,804
  • 8
  • 40
  • 61