Results 1 to 5 of 5
- 08-17-2009, 11:48 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Static is a pain in the ass word!
Hi, I just cant manage to figur out what static stands for, all I know is that it's the shittiest word in java.
It keeps causeing so freakn unbelivable many error that Im about to shit my pants! :mad:
Can someone explain in clean english why this doesnt work:
cause I keep getting the error:PHP Code:class ExtendingTesting { private static int valve = 0; void changeValue(int c){ this.valve = c; } void showValue(){ System.out.println("Value: "+this.valve); } } class hi extends ExtendingTesting { public static void asa (){ int a = 10; hi.changeValue(a); hi.showValue(); } }
It's a true pain in the ass, can someone help me?non-static method changeValue(int) cannot be referenced from a static context
- 08-18-2009, 12:01 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
hi.changeValue() isn't static, but you're calling it like it is.
- 08-18-2009, 12:41 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 8
- Rep Power
- 0
Correct me if I'm wrong...
I was taught that static means the method/class/whatever does not work with/on an object of any sort.
However, I've never quite understood when a method can be declared static and when not.
I do know that it should work if you get rid of the 'static' in your program. A method can be static without you telling it it is, right? ;)
- 08-18-2009, 01:19 AM #4
Static means that something that is static does not require object instantiation to be used. You make a method or variable static when you want to use it outside of the class without making a new instance of that class. For example:
This works because the pi field is static. If pi was NOT static, you would have to have done it this way:Java Code:class Numbers{ static double pi = 3.141592653589793D; } class Test{ public void print(){ System.out.println(Numbers.pi); } }
This is true for methods as well. Static is a wonderful tool when you know what it does and how to use it -- also, it is used almost everywhere in the java class library.Java Code:class Numbers{ double pi = 3.141592653589793D; } class Test{ public void print(){ Numbers num = new Numbers(); System.out.println(num.pi); } }
The only thing you should make static as a new java programmer is a main method:
Main methods must be static, but avoid it elsewhere until you have a real need for it and a deeper understanding of it.Java Code:public static void main(String[] args){ //do something }
- 08-18-2009, 01:23 AM #5
Also, please read my post to a similar question on another thread. I explained static there too and gave some other examples:
Static and non staic reference, Synchronized and unsynchronized, abstarct class,
Similar Threads
-
SpringLayout ... Pain!
By new_2_java in forum New To JavaReplies: 4Last Post: 11-10-2008, 04:59 PM -
Text-pain
By willemjav in forum Java AppletsReplies: 5Last Post: 06-21-2008, 10:01 AM -
Explicit static initialization with the static clause
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:07 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks