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?
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?
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".
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
}
}
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 .
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.
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.
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.
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^^^^^^^");
}
}
You can declare everything static. But what kind of normal scale object model would need all methods static , is a big question
Yes you can depends upon your need. But remember "this" keyword will not work in this case you can not access to public members!