Results 1 to 5 of 5
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Thumbs down 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:

    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();
    	}
    }
    cause I keep getting the error:
    non-static method changeValue(int) cannot be referenced from a static context
    It's a true pain in the ass, can someone help me?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    hi.changeValue() isn't static, but you're calling it like it is.

  3. #3
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    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? ;)

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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:
    Java Code:
    class Numbers{
        static double pi = 3.141592653589793D;
    }
    
    class Test{
        public void print(){
            System.out.println(Numbers.pi);
        }
    }
    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{
       double pi = 3.141592653589793D;
    }
    
    class Test{
        public void print(){
            Numbers num = new Numbers();
            System.out.println(num.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.

    The only thing you should make static as a new java programmer is a main method:

    Java Code:
    public static void main(String[] args){
        //do something
    }
    Main methods must be static, but avoid it elsewhere until you have a real need for it and a deeper understanding of it.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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

  1. SpringLayout ... Pain!
    By new_2_java in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 04:59 PM
  2. Text-pain
    By willemjav in forum Java Applets
    Replies: 5
    Last Post: 06-21-2008, 10:01 AM
  3. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •