java: static and nonstatc function calling approach -
i have class:
public class c1{ public int v=10; public int myfunction1(){ // code } } to call myfunction1() use:
c1 ob = new c1(); ob.myfunction1(); ob.v; same thing `static':
public class c1{ public static int v=10; public static int myfunction1(){ // code } } to call myfunction1() use:
c1.myfunction1(); c1.v; so question difference between these 2 approach. when use static approach? technical advantage , disadvantage of both?
the difference best illustrated if change example somewhat:
public class c1{ private final int v; public c1(final int v) { this.v = v; } public int getv(){ return v; } } in case when create c1 give internal state - i.e. v. can do
final c1 firstc1 = new c1(10); final c1 secondc1 = new c1(20); you cannot static methods bound class instance , not object instance - hence change in state seen all method calls.
generally, in oo design, static best avoided. has places, it's used utility classes , constants (although in java enums better constants).
Comments
Post a Comment