0

I have a class:

public class C1{
  public int v=10;
  public int myFunction1(){
       // some code
  }
}

To call myFunction1() we use:

C1 ob = new C1();
ob.myFunction1();
ob.v;

Same thing with `static':

public class C1{
  public static int v=10;
  public static int myFunction1(){
       // some code
  }
}

To call myFunction1() we use:

C1.myFunction1();
C1.v;

SO my question is what is the difference between these two approach. When its good to use static approach? What are the technical advantage and disadvantage of both?

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147

5 Answers5

2

The difference is best illustrated if I change your example somewhat:

public class C1{
    private final int v;

    public C1(final int v) {
        this.v = v;
    }

    public int getV(){
       return v;
    }
}

In this case when you create your C1 you give it an internal state - i.e. v. Now you can do

final C1 firstC1 = new C1(10);
final C1 secondC1 = new C1(20);

You cannot do this with static methods as they are bound to the Class instance and not the Object instance - hence any change in state would be seen by all method calls.

Generally, in OO design, static is best avoided. It has its places, it's often used for utility classes and constants (although in Java enums are better for constants).

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • `All static variables are initialized before any object can be created.` what would you say about this? If true then doesnt it make it memory consuming – Manish Kumar Oct 06 '13 at 16:50
  • 1
    @Manish all variables need to be initialised at some point. In fact `static` variables are maybe _less_ memory consuming, there is only one instance per `Class`. Often expensive resources (thread safe ones) are stored in `private static` variables. – Boris the Spider Oct 06 '13 at 16:52
1

If C1 has no internal state (fields) that you want to vary among instances, then there's not a whole lot of difference. But then you should be using a singleton pattern (because all instances of C1 are basically identical). If v will vary among instances, or if subclassing might create distinctions between instances of C1, then the static approach usually doesn't work out very well.

The main reason to use a singleton instead of a static approach is that the singleton can be declared to implement various interfaces.

For more discussion, see this thread and this one.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

static members are shared by all the instances so if you create two instance then both of htem will share the value of v. There is no advantage of disadvantage of static/non-static, it just depends on the need. If you want the value of an attribute to be shared by all the instances then mark it as static and if you want each instance to have its own value of the attribute then mark it as non-static.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

static methods are first to load in stack of memory. If you have noticed the main method of a class is itself static [public static void main]

All instances of the class share static variables. All static variables are initialized before any object can be created.

A static method cannot access non-static variable.

A static method IS GOOD for a utility method that does not depend upon particular instance.

Javanator
  • 9
  • 3
0

it has so much difference. both class definition with non static and static members are useful in different scenario.we all know static member variable is shared by all instance of that class. so,in second case when public static int v = 10;

suppose one change it to 20 then all instance of class C1 see this value 20. which is not possible in first case where public int v = 10;

in this case all instance of C1 has own copy of v which is initialize to 10.and one instance change value of v 10 to some other value not make effect of other instance v value.

similarly to static method.we all know static methods can only access static member's. so making method to static or non static depend on your requirement.

vikas singh
  • 131
  • 1
  • 2
  • 10