• 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

Accessing private class variables...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok lets say I have a class called AltTreeMap. In that class one of the class variables is: private Node root. Now in my node class I want to access the root in this fashion: if (root==null)..

how do I do this? If I try using this.root the compiler thinks "this" refers to a Node, not to the AltTreeMap class. If I try type casting it like this: ((AltTreeMap)this).root the compiler tells me they are unconvertible types

thanks for the help!
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you can post some code to explain where you declare what/want to access what?

Helmut
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a few choices, from best to worst as far as my views of design are concerned.
  • Have AltTreeMap manage its root and Nodes -- not the Node class.
  • Expose root with an accessor in AltTreeNode: public Node getRoot() { return root; }
  • Give root package scope (no private/public/protected keyword) and put both classes in the same package. Now Node can access root.
  • Make root public so everyone has read/write access to it.

  •  
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Alex Almani:
    If I try using this.root the compiler thinks "this" refers to a Node, not to the AltTreeMap class. If I try type casting it like this: ((AltTreeMap)this).root the compiler tells me they are unconvertible types


    This is because "this" refers to the "current object" so to speak. Inside the class Foo, "this" refers to an instance of Foo, but inside another class "Bar", "this" refers to an instace of Bar. Or in your case, inside the class Node, "this" refers to an instance of Node. Since Node and AltTreeMap are not related by an inheritence hierarchy, the cast you use will fail.

    I hope this clears some confusion rather than causing more.

    Good Luck and Keep Coding!

    Layne
     
    Ranch Hand
    Posts: 195
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The simplest way would be to have an accessor method in your AltTreeMap class and have it return 'this.root'. Try it.
    reply
      Bookmark Topic Watch Topic
    • New Topic