Reputation: 1435
I'm a total noob at java, but while practicing tonight it occurred to me that with OOP design every method in the main class is going to have to be static right? In this code there is no way for me to call a method within the class that isn't static.
It seems like maybe I'm missing the point of why you would declare a class static or not. Thanks for your help!
public class JavaApplication2 {
private static CreateCar Vroom;
private static Limo Fuuu;
public static void main(String[] args) {
Vroom = new CreateCar();
Vroom.creator();
getGas();
addGas();
getGas();
Fuuu = new Limo();
Fuuu.creator();
Fuuu.wheels = 5;
Fuuu.wheelie();
}
public static int getGas(){
Vroom.returnGas();
return 0;
}
public static void addGas(){
Vroom.fillerUp();
}
}
Upvotes: 26
Views: 65921
Reputation: 530
method is declared as static, so as to be referred by class name... eg
Math.pow(x,y) // x^y
In this method called user don't have to worry about instance creation... as Math has all its method as static so thats the basic reason why static methods are used....
static method can call non-static methods though object of class
ClassName cn =new ClassNam().methodName;
main() is special method, its starting point of execution of the program, so u can never have a object without running the program, and hence its called like(execution:: java ClassName), so it must be static...
Upvotes: 0
Reputation: 93050
You can call non-static methods, but you can only do so through an object. That is, you need to call the method on a given object.
Your main class can also be instantiated, so not every method in the main class needs to be static. For example:
public class MainClass {
int value;
public void printValue() {
System.out.println("" + value);
}
public static void main(String[] args){
MainClass o = new MainClass();
o.printValue();
}
}
Upvotes: 56
Reputation: 4109
Static methods mean that you can call it directly through
public class ClassName{
//no-op
public static void method(){//....}
}
ClassName.method();
And, in some cases, the ClassName
can be omitted eg.
static import
main method
But to invoke a non-static method, you must first initiate it.
Upvotes: 0
Reputation: 24641
short answer: no, every method does NOT need to be static.
in fact:
public static void main (String [] argv) { /* ... */ }
Upvotes: 0
Reputation: 12726
No. But the main
method have to be static
.
To call a non-static method in the class, you have to create a reference to an object of that class. Then you call the method from the reference.
public class JavaApplication2 {
// non-static method
public void go() {
System.out.println("hello");
}
public static void main(String[] args) {
// declare a reference
JavaApplication2 app;
// create an object
app = new JavaApplication2();
// call a method
app.go();
}
}
Upvotes: 9
Reputation: 76908
Er, you don't have a "static Main" class. You have a static method named main
in your JavaApplication2
class.
There's no requirement to have any other static methods in that class. To use the class, you'd instantiate it:
public static void main(String[] args)
{
JavaApplication2 myApp = new JavaApplication2();
myApp.someMethod();
...
}
main
is a specifically named static method that acts as an entry point. When you tell Java to "run" your program from the command line like:
java JavaApplication2
The JVM calls JavaApplication2.main()
passing in the command line arguments.
Upvotes: 3
Reputation: 121799
Short answer: yes.
The reason is that your JVM needs to invoke your class'es "main" independent of already having any existing instances of that class.
Minor semantic nit-picking: "main" is a member (a static member) of a class; it isn't the name of the class itself.
Useful tip: each class can have it's OWN, DIFFERENT "main()". This can be extremely useful for unit-testing individual classes.
This sometimes comes as a shock to C/C++ programmers, where "main()" is associated with the ".exe", and you can only ever have one of them. In java, you can have many "main()" methods, and invoke whichever one you choose at runtime.
Upvotes: 6