Forums Register Login

whether private static variable can be accessed out side the class

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
"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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 ]
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1122 times.
Similar Threads
Kindly explain this code from SCJP faq
Inner Classes
Private access in InnerClass
private & static
Why i can access static variable with Class Instance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:00:25.