5

I was being questioned by this, why can't I declare all methods static? Can you give me a piece of explanation in here? Thanks.

I believe that when you make a method static, it cannot access non-static members?

9 Answers9

5

You can. There would be no reason to instantiate objects from such a class. Any state would be global across the JVM.

Your questioner was perhaps confusing "can't" with "shouldn't".

Synesso
  • 37,610
  • 35
  • 136
  • 207
  • *Any state would be global across the JVM.* nay, you can have normal classes w/ static methods that take the said classes and access the fields (just like C struct). As a "benefit" there won't be overridden methods – bestsss Jul 21 '12 at 20:54
  • OK, granted. Also `ThreadLocal`s. – Synesso Jul 22 '12 at 12:15
4

Static methods cannot access instance variables. :)

public class MyStaticExample{
  private String instanceVariable = "Hello";
  private static String STATIC_VARIABLE = "Hello too";

  public static void staticMethod(){
    System.out.println(this.instanceVariable); // this will result in a compilation error.
    System.out.println(STATIC_VARIABLE); // this is ok
  }

  public void instanceMethod(){
    System.out.println(this.instanceVariable); // this is ok
    System.out.println(STATIC_VARIABLE); // this is ok
  }
}
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
2

You can declare all method as static but then your method will become class level. Let say you create 10 objects have different states where state is held by properties. Then in this case you will not be able to get the state of objects by using static methods .

Object obj;
public static Object getState(){

    return obj; // compile time error Cannot make a static reference to the non-static field 
}

Making all methods static is good practice in Utility Classes .

Irshu
  • 8,248
  • 8
  • 53
  • 65
amicngh
  • 7,831
  • 3
  • 35
  • 54
2

You can. But the result would be a structured program (think C, Pascal) using existing classes, not real OOP. Object orientation is the idiomatic approach to Java programming, so keep the number of static keywords in your code low.

2

I would start with Lesson: Object-Oriented Programming Concepts and pick almost any class from the JDK. A simple one to start with is Boolean which needs two instances, one for TRUE and one for FALSE which can't be implemented using static methods.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

There is no restraint like that. You can make all methods in a class static. In C# you can even define a class itself as static (requiring all methods in it to be static).

It is more a question of what you want to achieve with that class.

Jonas Eicher
  • 1,413
  • 12
  • 18
1

You can define all methods as static. It depends on the context whether you should or shouldn't. Best option is to make static library method. For example below is a TestHelper utility class I have created-

public class TestHelper {
    /**
     * Method echo.
     * 
     * @param heading
     *            String
     */
    public static void echo(String heading) {
        System.out.println("+++++++++++++++++++++++++");
        System.out.println("++++++ " + heading + " ++++++");
    }

    public static void end() {
        System.out.println("=========================");
    }

    public static void mark() {
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    }

    /**
     * Method spot.
     * 
     * @param string
     *            String
     */
    public static void spot(Object string) {
        System.out.println("*****_______" + string + "_______******");
    }

    /**
     * This is a stub method
     */
    public static void stub() {

    }

    public static void error(String string) {
        System.out.println("^^^^^^^^^^^Error Begining^^^^^^^^^^^^^^");
        System.out.println(string);
        System.out.println("^^^^^^^^^^^Error End^^^^^^^");
    }
}
Chan
  • 2,601
  • 6
  • 28
  • 45
0

You can declare everything static. But what kind of normal scale object model would need all methods static , is a big question

kommradHomer
  • 4,127
  • 5
  • 51
  • 68
  • Why not, wouldn't it save you the time to make objects? Can't you just call methods using the class instance that way? And access variables using the class instance? What is the point of objects then? – Ruchir Baronia Jan 03 '16 at 00:07
  • http://stackoverflow.com/questions/34572117/why-dont-we-make-everything-static – Ruchir Baronia Jan 03 '16 at 00:44
0

Yes you can depends upon your need. But remember "this" keyword will not work in this case you can not access to public members!

Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36