-6

Unless the class is private or part of the Java API, I don't see using static as advantageous as part of OOP.

The only time I see using static being useful is when you need an object incrementer every time you create a new object of the same type.

For example:

public class Duck {
    static int duckCount = 0;
    private String name;

public Duck(String name) {
    this.name = name;
    duckCount++;
    }
}

public class DuckTesting {
public static void main(String[] args) {
    System.out.println(Duck.duckCount);
    Duck one = new Duck("Barbie");
    System.out.println(Duck.duckCount);
    Duck two = new Duck("Ken");
    System.out.println(Duck.duckCount);
  }
}

OUTPUT:
0
1
2

UPDATE: Useful answers found in Java: when to use static methods/variable

  • In the opposite direction - Why would it be useful to recreate the same variable over and over and over if every instance of an object should have the same variable with the same value? – takendarkk Aug 14 '17 at 18:49
  • static modifier is not an advantageous part of OOP. It is rather a trade off that makes sense. Why something that is not specific to an instance of a class should be stored in each in each object of a class ? It is misleading, not efficient and useless. – davidxxx Aug 14 '17 at 18:52
  • 3
    Possible duplicate of [Java: when to use static methods](https://stackoverflow.com/questions/2671496/java-when-to-use-static-methods) – 17slim Aug 14 '17 at 18:52
  • Read the posting of [Java: when to use static methods](https://stackoverflow.com/questions/2671496/java-when-to-use-static-methods). I understand it a little better now. Sorry if my question does sound too broad. – Nayema Nasir Aug 16 '17 at 15:55

3 Answers3

1

-First use: so here's a little exemple : let's say we need to create people profiles, so we'll make a class named profile and we need that every profile to have an id

public class Profile{
int id;
String name;

public Profile(String name,int id)
{this.name=name;
this.id=id;}
}

the probleme here is : how to make a default id , and every time we create a profile , it will increase and have it's own personal id ??like this :

profile 1 :id=1
profile 2 : id=2 
profile 3: id=3 

without creating it manually !

To have this we need a static variable ,it means that this Variable will be shared by all the objects from the same class : if this variable equals 1 , it means in all the others objects that have the same class will have it equals 1!

let's write our class to have this

public class Profile{
int id;
String name;
//here is it! the first time it will have 1
static int idIncreaser=1;


public Profile(String name)
{this.id=this.idIncreaser; //so the first time the object will have id=1
this.idIncreaser++; //we'll increase it so the next created object will 
                    // have id=2
this.name=name;
}
}

and about a static method, we make it static if we will use it in the main Class or we want it to do the same work like our idIncreaser

-Second use: if you have a static variable or a static method, you are able to acces it without the creation of objects, it means that you can do:

int a=Profile.idIncreaser; //note that the 'p' is in uppercase to be able to acces the class !

In General: note that a static variable/method is shared by all the objects of this class, and can be accessible witout the instanciation or creation of any object

DetchKing
  • 185
  • 1
  • 12
0

Static methods in OOP should use only for "builders", when you don't like new keyword:

public class ABC {
    private final int a;
    private final int b;

    public ABC(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public static ABC with(int a, int b) {
        return new ABC(a,b);
    }
} 

you can instantiate ABC class as:

ABC abc = new ABC(1, 2);

or

ABC abc1 = ABC.with(1, 2);

what style you prefer. No more place for static methods in OOP. Also every private method - candidate for new class, because each private method not reusable. Also static constants useful for memory economy, speed up, when you create a lot of objects with same data inside.

0

static methods useful when you need to prevent class's instance (object) from use some methods in class and in same time you need to use this methods by the class itself