Results 1 to 10 of 10
- 02-04-2008, 10:22 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
Newbie question about Static methods
Hello everyone... need to ask u guys a question.
Why do we need the word static in the declaration of the main method ???
Now I see that the main method has to be public so the JVM can see it from outside and I KNOW that static means that its not necessary to create an instance of the method!. But why do we need that in the first place ???
Cant we just remove the static word and declare a new main method.. ???
This whole thing about static is really confusing me...
I hope somebody can explain that and I wish that maybe someone can post a simple little program illustrating the purpose of the static in methods and variables.
Hope u wont find this a dum question.. any help would be appreciated.
Thanks.
- 02-05-2008, 03:17 AM #2
Member
- Join Date
- Dec 2007
- Posts
- 30
- Rep Power
- 0
Here's my understanding about the difference between static and nonstatic methods...
Static methods will always be the same for every object they are in. So lets say you have a Circle class, you could have a static method called printAreaFormula().. which would always print: "3.14 * r * r" (or similar).
Nonstatic means that the object can be different. For the circle class you could have a circle object with a radius of 3 and one with a radius of 4. So if you have a printRadius() function the outcome would depend on the object.
The main method is static because it always does the same thing (which can vary with paramteres and user input)...
- 02-05-2008, 07:06 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
I see your point here... but why are we declaring it as a static method ???
In your example we still can passs values to the parameters even if we dont declare the method (static). I read more than a Java book and they all mention that "Static means that you dont have to create an instance or an object of that static method in order to call it or use it"... and I still dont see what does it mean.. ??
And this leads me to another question... is it possible to change static methods or variables ???
Thanks alot for the example.
- 02-05-2008, 07:20 AM #4
Member
- Join Date
- Feb 2008
- Posts
- 13
- Rep Power
- 0
The Word static is used where we want to execute a method without instatiating an object.
we can write a class without main(). By keeping static block inside class as:
class ProgrmaWithoutMain
{
static
{
System.out.println("Program with out main()");
System.exit(0);
}
}
- 02-05-2008, 09:26 AM #5
Member
- Join Date
- Feb 2008
- Location
- South Africa
- Posts
- 9
- Rep Power
- 0
A method is declared as static in the following cenario's:
1. The main method
2. No instantiation is required.
3. No Method over-riding will occur.
4. All instances of a class will use the same method...thus its not necessary
to allocate unnecessary memory to duplicate the same object(method,
class or variable) has to do with reduction in overall redundancy and
improvement of memory management.
Then to answer your question regarding changing static variables and methods SCS17..
As I understand it values can be sent as arguments to static methods thus the parameters(the variables that receive values sent to a method) will change but not the method itself.If that makes any sense to you..
If you are looking to create totally unchangable classes, methods and variables use the final key word instead.
If possible could a more seasoned developer please shed some light on this post as im sure that I might have missed something.
- 02-05-2008, 09:40 AM #6
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
This is how i understood why main method is static..
jvm starts execution of the code from main method of the class ...
To execute the same main method no matter how mant times the class is being executed we use static..
final does not this property.. it only says the method cannot be inherited..
- 02-05-2008, 09:51 AM #7
Member
- Join Date
- Feb 2008
- Location
- South Africa
- Posts
- 9
- Rep Power
- 0
Praveena,
You are correct such a method or class cannot be inherited , and it is impossible to add final to the main method and unnecessary as final is used to set an object once and never be able to change again after that eg: if I declare a final variable to contain the fixed interest rate for a banking transaction and do not want this value to change after it has been set then id use the Final keyword.
Gib
- 02-05-2008, 11:38 PM #8
Member
- Join Date
- Dec 2007
- Posts
- 30
- Rep Power
- 0
The specific part about it not being instantiated would probably be more clear with an example. Look at the Math class... You can use methods such as Math.pow(4,2), pow is a static method (I've never checked this but I think it makes sense). Just like Main(stringVariableOfArguments) is calling a Main method of a class with an argument.
- 02-06-2008, 07:18 AM #9
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
so if we take a look at the Scanner method.. each time we want to use it.. we have to declare a Scanner object.. say
Scanner input = new Scanner(System.in)...
so I suppose Scanner here is non-Static.. since we decalred an object called input... am i thinking right here ??? Hope so!
it makes a little more sense now.. i see your point guys.. but I still cant understand why the main method has to be Static.
Thanks alot for the help!.
- 02-06-2008, 08:03 AM #10
Member
- Join Date
- Feb 2008
- Location
- South Africa
- Posts
- 9
- Rep Power
- 0
Yes you are right, Scanner would be non-static in that case.
The reason that Main is static is that all instances of a class will use the same point of entry...look at it from a performance point of view, why would I want to create a duplicate of a method that will never change and be used only once in the lifetime of an instanciated class..
Does it make more sense?
Similar Threads
-
Abstract Class with Static Methods
By bugger in forum New To JavaReplies: 7Last Post: 09-05-2008, 12:20 AM -
Static methods - not working
By Echilon in forum New To JavaReplies: 2Last Post: 12-21-2007, 01:31 PM -
Why methods in an interface cannot be static?
By cbalu in forum Advanced JavaReplies: 2Last Post: 12-12-2007, 07:57 PM -
Static methods
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:56 PM -
significance of static variables and methods
By imran_khan in forum New To JavaReplies: 4Last Post: 08-02-2007, 09:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks