Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2008, 12:22 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-05-2008, 05:17 AM
Member
 
Join Date: Dec 2007
Posts: 30
Leprechaun is on a distinguished road
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)...
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-05-2008, 09:06 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
Quote:
Originally Posted by Leprechaun View Post
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)...
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-05-2008, 09:20 AM
Cnu Cnu is offline
Member
 
Join Date: Feb 2008
Posts: 13
Cnu is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-05-2008, 11:26 AM
Member
 
Join Date: Feb 2008
Location: South Africa
Posts: 9
gibsonsydsa is on a distinguished road
Send a message via MSN to gibsonsydsa
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-05-2008, 11:40 AM
Member
 
Join Date: Jan 2008
Posts: 8
praveena is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-05-2008, 11:51 AM
Member
 
Join Date: Feb 2008
Location: South Africa
Posts: 9
gibsonsydsa is on a distinguished road
Send a message via MSN to gibsonsydsa
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-06-2008, 01:38 AM
Member
 
Join Date: Dec 2007
Posts: 30
Leprechaun is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-06-2008, 09:18 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
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!.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 02-06-2008, 10:03 AM
Member
 
Join Date: Feb 2008
Location: South Africa
Posts: 9
gibsonsydsa is on a distinguished road
Send a message via MSN to gibsonsydsa
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract Class with Static Methods bugger New To Java 7 09-05-2008 02:20 AM
Static methods - not working Echilon New To Java 2 12-21-2007 03:31 PM
Why methods in an interface cannot be static? cbalu Advanced Java 2 12-12-2007 09:57 PM
Static methods Java Tip Java Tips 0 11-04-2007 07:56 PM
significance of static variables and methods imran_khan New To Java 4 08-02-2007 11:52 AM


All times are GMT +3. The time now is 04:44 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org