Results 1 to 17 of 17
Like Tree4Likes
  • 1 Post By PhHein
  • 1 Post By Toll
  • 1 Post By PhHein
  • 1 Post By jim829

Thread: Static variable

  1. #1
    luky207 is offline Member
    Join Date
    Jul 2013
    Location
    Zagreb,Croatia
    Posts
    6
    Rep Power
    0

    Post Static variable

    Hello,I dont understand why i and c must be static if I want to use them in main method,if I remove prefix static compiler returns error.Thank you :)

    public class PrimitiveTest {
    static int i;
    static char c;
    public static void main(String[] args) {
    System.out.println("int = " + i);
    System.out.println("char = " + c);
    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Static variable

    Because the main method is static and thus can only access static members and methods.
    luky207 likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Static variable

    Because they are being referenced from a static method.

    Edit: TOO slow!

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Static variable

    Static means they can be used without having an instance. As such, static methods must only use static variables; non-static variables have no values before being instanced.

    Also to keep in mind regarding static variables is that they're shared between all instances of that class. Take this class for instance:

    Java Code:
    class DataCollection
    {
      static int i=0;
      int x=0;
    }
    If you use this class in a code, you can use DataCollection.i to get the i variable, but you can't use DataCollection.x in the same way. By creating an instance (i.e. new DataCollection), you'd be able to get both i and x through that instance. However, if you were to change the value of i through that instance, and then use DataCollection.i, you'd get the new value set, not the old.
    luky207 likes this.

  5. #5
    luky207 is offline Member
    Join Date
    Jul 2013
    Location
    Zagreb,Croatia
    Posts
    6
    Rep Power
    0

    Default Re: Static variable

    Quote Originally Posted by PhHein View Post
    Because the main method is static and thus can only access static members and methods.
    If I figured it out,main method is ALWAYS defined with static prefix ? And I can in main only use STATIC variables ?

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Static variable

    Yes and yes.
    luky207 likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Static variable

    Quote Originally Posted by luky207 View Post
    If I figured it out,main method is ALWAYS defined with static prefix ? And I can in main only use STATIC variables ?
    You can also use local variables, declared within the main(...) method, and of course the String array that is passed to the main(...) method as an argument.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Static variable

    Most of the time I prefer to simply create a method like, startup(), initialize(), begin(), or something similarly descriptive. Then I can call that method from main where I have easy access to the instance fields without having to prefix the class reference. And of course I can still create any local variables I need.

    E.g.
    Java Code:
    public class MyClass {
        // instance fields here
       public static void main(String[] args) {
           new MyClass().startup();
      }
       
      public void startup() {
           // Begin the processing here.
      }
    }
    Regards,
    Jim
    Last edited by jim829; 07-15-2013 at 05:22 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    luky207 is offline Member
    Join Date
    Jul 2013
    Location
    Zagreb,Croatia
    Posts
    6
    Rep Power
    0

    Post Re: Static variable

    Java Code:
    public class klasa1{
    	public static void main(String[]args){
    		class Prvaklasa{
    			static void funkcija(){
    				System.out.println("good");
    				}
    		}
    		Prvaklasa.funkcija();
    	}
    }
    Here I want to call method funkcija without creating object.As I read before it can only be done with static methods and static fields.
    But something here is wrong.The error is :modifier static is only allowed in constant variable declarations

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Static variable

    The problem is that you are trying to create a static method in a nested, non-static class. That is not allowed. In this case remove static from your method funkcija(). Of course, then you will have problems referencing the method statically, so try this.

    Java Code:
    public class klasa1 {
       public static void main(String[] args) {
          Prvaklasa.funkcija();
       }
    
       static class Prvaklasa {
          static void funkcija() {
             System.out.println("good");
          }
       }
    }
    Regards,
    Jim
    Last edited by jim829; 07-15-2013 at 06:36 PM.
    luky207 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    luky207 is offline Member
    Join Date
    Jul 2013
    Location
    Zagreb,Croatia
    Posts
    6
    Rep Power
    0

    Default Re: Static variable

    Quote Originally Posted by jim829 View Post
    The problem is that you are trying to create a static method in a nested, non-static class. That is not allowed. In this case remove static from your method funkcija().

    Regards,
    Jim
    Thank you Jim,but that there is still and error,If I change inner class to static the error message is : illegal start of expression
    And If I remove static from my method then the problem is that you can't call method by class name if the method isn't static.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Static variable

    Nuts. I was editing my reply while you were replying to my unedited post. I caught that and made a recommendation.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Static variable

    Quote Originally Posted by luky207 View Post
    If I change inner class to static the error message is : illegal start of expression
    Read the code given by Jim. It's only 11 lines. Notice that he starts the nested class definition not inside the main(...) method, but after its closing brace.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Static variable

    Quote Originally Posted by luky207 View Post
    Here I want to call method funkcija without creating object.
    May I ask why? Java is an object oriented language; I see no reason to be averse to constructing objects.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    luky207 is offline Member
    Join Date
    Jul 2013
    Location
    Zagreb,Croatia
    Posts
    6
    Rep Power
    0

    Default Re: Static variable

    Quote Originally Posted by jim829 View Post
    Nuts. I was editing my reply while you were replying to my unedited post. I caught that and made a recommendation.

    Regards,
    Jim
    Jim ,what that solution means is that I cant create classes inside main method ?
    Last edited by luky207; 07-15-2013 at 07:41 PM.

  16. #16
    luky207 is offline Member
    Join Date
    Jul 2013
    Location
    Zagreb,Croatia
    Posts
    6
    Rep Power
    0

    Default Re: Static variable

    Quote Originally Posted by DarrylBurke View Post
    May I ask why? Java is an object oriented language; I see no reason to be averse to constructing objects.

    db
    I've never programmed in Java before,so I want to learn how to do everything in every possible way.
    And is there some reason why cant I create static class in main method ?

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Static variable

    Ok, here's my take on this.

    Method variables are supposed to be local and thus inaccessible outside their scope. But if you had two methods declaring the same variable as static, it would be a duplicate declaration and cause a problem because static variables are class level variables. If you decide that a static method variable is not a class level variable then how to you assign it? The same would be similar for static classes. Two identically named static classes declared in two different methods would cause problems. So if you can only do one, then where is the benefit? And static nested classes are really no different than top level classes. They are just more conveniently packaged.

    Note: One possible use of a static local variable might be to support a closure capability. But closure will be available as a feature in Java 1.8.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 12:46 AM
  2. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  3. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 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
  •