Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By stuckonjava

Thread: static question

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default static question

    public class question {

    private int num = 5 ;



    public static void main (String[] args){


    System.out.print(num);


    }



    }


    Can someone explain to me why the num variable has to be declared as static to be access by the main method, also why can I not do this?
    public class question {

    private static int num = 5 ;

    num = num + 2;

    public static void main (String[] args){


    System.out.print(num);


    }



    }


    Thanks for your time

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: static question

    1. The num variable is a non static field. Which mean can only accessed through an instance of of class Question. When you access it directly from the main() method you can't. Because the main() method is a static method. Static method can only access static field. To have access to the num variable you have to create an instance of Question and access it from this object.

    2. You can't do num = num + 1; an expression should be within a block of statement.

    You can read for details here: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: static question

    Quote Originally Posted by wsaryada View Post
    1. The num variable is a non static field. Which mean can only accessed through an instance of of class Question. When you access it directly from the main() method you can't. Because the main() method is a static method. Static method can only access static field. To have access to the num variable you have to create an instance of Question and access it from this object.

    2. You can't do num = num + 1; an expression should be within a block of statement.

    You can read for details here: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    What do you mean by num = num +1; should be within a block of statement?
    Do you mean like in a method? Thanks
    sabarinathan likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

Similar Threads

  1. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  2. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  3. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 AM
  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
  •