Forums Register Login

accessing private variable and private constructor using static method

+Pie Number of slices to send: Send
In this code, I can create a new object of StaticTest class using static method getTStaticTest(). But I can not access static int i, using static method getI(). Does it mean that all constructors are implicitly static ? and thats why we can access private constructor but not non-static private variable ?

Thanks
+Pie Number of slices to send: Send
variable i is an instance level variable of class. this variable can be accessed only from object level method if you want to use it without prefixing it with any object reference.
Non static private variable are only accessible in object level context/methods.
Static method exist at class level .. so they do not allow usage of any instance variable directly (without attaching object reference to it).


Object creation is not dependent on static context/access restriction. You can create object in an static method.
Constructor are never static. Constructors are used for object creation.

Everybody's invited. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1809 times.
Similar Threads
Order of Static initialization Block,Instance initialization Block and Constructor....
Static variable initialization
Looking for explanation
Private variables inherited but not accessible
compile-time errror
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:18:42.