Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Variable Scope

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable Scope

    Hi, can you answer me this.

    With class variables (a variable which is not declared in a body of any method), what are their scope?

    And why do I have to put "public static" in front of class variables, should they not be available to all methods already?


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Variable Scope

    An instance variable of a class has scope that is defined by the scope of the object created (although - depending upon the access modifier, may or may not be accessed directly). static makes a variable 'constant' across all instances of a class, and does not require an class to be instantiated to be accessed - there is a big difference between an instance variable and a class variable.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Scope

    So we make class variables a static variable so that we can use it without instantiating it?

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Scope

    If so, then why dont we make variables inside methods static also?
    Last edited by TP-Oreilly; October 5th, 2011 at 02:00 PM.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Variable Scope

    Quote Originally Posted by TP-Oreilly View Post
    If so, then why dont we make variables inside methods static also?
    A static variable has the same value across all instances. If you want values to differ from one instance to the next, then static isn't appropriate. This is more often the case. As an extreme example, take the String class...imagine if the data within this class were declared static - one could never have 2 string's with different values.

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Scope

    I understand that the static keyword makes a variable have the same value across all instances, but I dont understand why when I put...

    import javax.swing.JOptionPane;
    public class Scope2 {
     
    	static int b = 20;
     
    	public static void main(String[]args){
     
    		JOptionPane.showMessageDialog(null, "The number " + b);
    	}
    }

    ...I have to put the "static" keyword in front of the "int b = 20;", seeing as we dont have to put the "static" keyword in front of variables created inside methods?

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Variable Scope

    Because the main method is static...and there is no instance of Scope2 created (you need to create an instance of Scope2 for b to be available - if it is not static). For example, a non-static variable
    import javax.swing.JOptionPane;
    public class Scope2 {
     
    	int b = 20;//not static
     
    	public static void main(String[]args){
    		Scope2 scope = new Scope2();     
    		JOptionPane.showMessageDialog(null, "The number " + scope.b);
    	}
    }

  8. #8
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable Scope

    Ok, thanks. So basically we use the static keyword for two reasons. 1. to make a variable have the same value across all instances and 2. to allow variables to be used without creating an instance of a class, which is why we can use the variables without using it from an object.

    Correct?

Similar Threads

  1. Java Scope in future
    By Shemil in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 04:45 PM
  2. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM
  3. What does SCOPE actually mean in java language?
    By wAdeski in forum Java Theory & Questions
    Replies: 3
    Last Post: December 1st, 2010, 09:44 AM
  4. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  5. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM