It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,
visit the help center.
Closed 8 years ago.
I'm just trying to see if I can fully understand the concept of static and the reason of static in a main class. The keyword static refers to the main class. the reason why methods in a main class are static is because the main class does not deal with objects but with the class itself.
However constructors deal with objects and therefore uses non static constructors because objects have unique characteristics and it would not make sense to make them static.
If anyone can see if I made a mistake in my statement or can steer me in the right direction, it would help me a great deal! :)
I'm just trying to see if I can fully understand the concept of static and the reason of static in a main class.
There is no such thing as a main class in Java.
The keyword static refers to the main class.
No, it refers to static classes or static class members.
The reason why methods in a main class are static is because the main class does not deal with objects but with the class itself.
There is no such thing as a main class. The statement is meaningless.
However constructors deal with objects and therefore uses non static constructors because objects have unique characteristics and it would not make sense to make them static.
All constructors are 'non-static'. There is no such thing as a static constructor. There is no point in any of this discussion.
If anyone can see if I made a mistake in my statement or can steer me in the right direction, it would help me a great deal! :)
I think you need to start again, forgetting about non-existent 'main classes' and 'static constructors'. Basically static methods refer to methods that can be invoked without having an instance of the class. Conversely, constructors create an instance of the class so they can't logically be static.
From Java Specification (Third edition):
About static fields http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.1.1
If a field is declared static, there exists exactly one incarnation of
the field, no matter how many instances (possibly zero) of the class
may eventually be created. A static field, sometimes called a class
variable, is incarnated when the class is initialized
About static methods http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.2
A method that is declared static is called a class method. A class
method is always invoked without reference to a particular object. An
attempt to reference the current object using the keyword this or the
keyword super or to reference the type parameters of any surrounding
declaration in the body of a class method results in a compile-time
error. It is a compile-time error for a static method to be declared
abstract.
Not exactly...
static
means the field/method etc belongs to the class, and not to a particular instance of the class. All instances of the class have access to static fields, and there is only one instance of each static field that is shared between instances.
The main method must be static, because it is invoked without creating an instance first.
All static methods may be invoked without creating an instance of the class - any methods the main method calls must also be static, unless the main method creates an instance of the class - then instance methods may be invoked on that instance.
public class MyClass { // This name will dictate the name of your file
public Sting myVariable = ""; // Field or Global variable
public void MyClass() { // Constructor
}
public static void main (String[] args){ // This is the "main" "Method"
MyClass.print("Hello "); // Static method call
MyClass myClass = new MyClass(); // Non-static method call
myClass.print("World");
}
public static void print(String s){ // Static method
System.out.print(s);
}
public void print(String s){ // Non-static method
this.myVariable = s; // Changing the value of a field/global variable
System.out.print(s);
}
}
STATIC METHOD CALL - When you make a static method call you do not make a lasting change to the data inside of the class/object.
NON-STATIC METHOD CALL - For this type of method call you must "instantiate" the object using the Constructor method (which cannot be static). An object can then be passed around. If your method changes the value of a field/global variable in the class then that value remains the same in that object until someone/something else changes it.
When you instantiate an object you can only "invoke" (call) non-static methods inside that object. Likewise you cannot call static methods from an object you must call it by providing the class name followed by a period followed by the method name.
Static methods can only reference other static content. Non-static can reference both.