• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private instance variables accessed within the class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code given below, eventhough the variables i and sampleString is declared as private the same is accessable directly from the object through the main method of test class. Does it mean that private/protected declaration is ignored within the class?

public class Test {
private int i = 25;
private String sampleString = "Arun";
public static void main(String[] args) {
Test test = new Test();
System.out.println("String is --" + test.sampleString);
System.out.println("Int value is --" + test.i);
}
}
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! Private members are accessible to the members of the class directly and through an object of the class...
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't access the private member outside the class anyhow directly.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes private member is accessible in the class
 
Arun Krishnan Nair
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes sweety..but it was a new knowledge for me that even if create object the private variables can be accessed using . operator

Thanks for all the replies
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private members of a class are accessible directly to all members of a class but from static members you cannot access directly but instead can access by creating an object.

eg
class Test
{

private int x;

void getX()
{
System.out.println(x};
}
public static void main(String[] args)
{
Test t;
System.out.println(t.x); //since main is a static method
}
}
[ September 03, 2008: Message edited by: Sirisha Ghatty ]
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sirisha Ghatty:

class Test
{
private int x;

public static void main(String[] args)
{
Test t;
System.out.println(Test.t); //since main is a static method
}
}



I think you were trying to write t.x instead of Test.t
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup its t.x
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic