• 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

Access private members from a constructor of another class?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this class with 2 private members,

Then I try to create a constructor links to that class, and I get stuck accessing those private variables.

I know that private member must be accessed by only it's class, but I can't figure out any way to do it, I try to create setter from MyClass but it still doesn't work


What I am missing?
 
Ranch Hand
Posts: 417
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hai Thompson wrote:I have this class with 2 private members,

Then I try to create a constructor links to that class, and I get stuck accessing those private variables.

I know that private member must be accessed by only it's class, but I can't figure out any way to do it, I try to create setter from MyClass but it still doesn't work


What I am missing?



Hello,



Maybe a cleaner way::


Alternative way:

 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is brilliant! Thank you very much! My problem is resolved.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would add a constructor to your first class and use that to set the variables. That way the object will be immutable, which is normally a good thing.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:I would add a constructor to your first class and use that to set the variables. That way the object will be immutable, which is normally a good thing.


I did what you said, do you mean this?

I don't know when I should initialize object in declaration and when I should not (like above).
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Setting x and y to zero is not necessary, but it doesn't hurt anything.


This makes your object immutable, that is, once it's created it does not change (as long as you don't add setters).
 
reply
    Bookmark Topic Watch Topic
  • New Topic