Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

whether private static variable can be accessed out side the class  RSS feed

 
aaaaaaaa aaaaaaaaaaaa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
you please check the following program:

public class ScopeTest
{
private static int i = 100;
public static void main(String[] args)
{
System.out.println("Hello World!");
System.out.println(i);

}
}


Even though the static variable i is private , it is perfectly accessible out side the class. According to my knowledge, private variable should not be accessed out side the class , but how could it happen?

I request you to please provide the answer as early as possible.

regards
Yugandhar Akkaldevi
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First: I think this is the wrong forum for this question and this should be moved to maybe JIG (beginning)

Second: Your code does not show you accessing i from outside the class you are accessing i from inside your class.

Third: In the future you should consider using code tags to keep the code formatted.
 
Saneth Dharmakeerthi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"System.out.println(i)" is inside ScopeTest class, so it's ok .
..........................

if you try this

class ScopeTest
{
private static int i = 100;
}

public class TestScopeTest
{
public static void main(String[] args)
{
System.out.println(ScopeTest.i);
}
}


you will get a error like this ...
TestScopeTest.java:10: i has private access in ScopeTest
System.out.println(ScopeTest.i);
^
1 error
..............

so the variable "i" is private it can only access inside the Class "ScopeTest"
[ May 13, 2007: Message edited by: Saneth Dharmakeerthi ]
 
Nomunbilegt Batsukh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

AFAIK, main method has access to private instance variables. Therefore the code should work the way you wrote it.


But, if you try to access the "i" from another class (I am not talking about an inner class here!), it won't work. That's what private is for.

I hope it helps.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nomunbilegt Batsukh:
Hi,

AFAIK, main method has access to private instance variables.


Not quite. main() is a static method, so it doesn't have access to variables that aren't static. Remember static members exist before any instances of the class exist, so static members can't refer to variables that don't exist.

More generally, any static method has access to all static variables whether they are private or public. Likewise, all methods that don't have the static keyword in front of them have access to all variables that don't have the static keyword in front of them. But additionally, methods that aren't static have access to all the static variables. Static members come into existence before any instances of the class exist. Therefore, if you create an object, its methods can access all the static variables that already exist. Here is an example of that:



Order of creation:

1) static methods and variables
2) instance methods and variables

Things in category 1) cannot access things in category 2) because they may not exist. However, things in category 2) can access things in category 1) because the things in category 1) have to exist by the time things in category 2) are created.

According to my knowledge, private variable should not be accessed out side the class , but how could it happen?

Which lines of code do you believe are outside your class? (Hint: anything that is between the opening brace of your class and the closing brace is inside the class.)

[ October 11, 2006: Message edited by: sven studde ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!