Results 1 to 6 of 6
Thread: Static variables and methods
- 04-25-2011, 12:13 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
- 04-25-2011, 12:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2011, 12:55 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It is creating objects that are local to the static method main. It's fine to do this, any method can create instances of objects locally for use in the method. Once the main method ends, the program is done.
For instance, these two are fine
It isn't referencing non static variables, it is creating it's on local object and using them.Java Code:public class X{ public static void XMethod(){ String s = "Hello"; System.out.println(s); } public void XMethod2(){ String s = "Hello"; System.out.println(s); } }
- 04-25-2011, 12:57 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Thank you.:)
- 04-25-2011, 12:57 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
static variables do not change between instances, meaning if you have two instances of class time.
- 04-25-2011, 02:07 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
You and I must've answered to entirely differen questions; I thought that the question was about this (sort of):
Oh well, as long as the OP is happy ;-)Java Code:public static void main(String[] args) { Whatever foo= new Whatever(); foo.x= 42; // <-- main( ... ) is allowed to do this while others aren't ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
cofusion with static and ordinary variables and methods
By sandeepsai39 in forum New To JavaReplies: 7Last Post: 09-02-2010, 12:25 PM -
Still having trouble with static & non-static variables
By Psyclone in forum AWT / SwingReplies: 6Last Post: 02-15-2010, 04:31 AM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
accessing instance variables from static methods
By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 10:09 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