8 Replies Latest reply: Dec 3, 2006 2:39 PM by 807599 RSS

    Accessing a Private Class

    807599
      Hi, newbie here so please forgive any information I give which is incorrect.

      I have two classes. I am able to open both of them. One class needs to access another.

      I have written what I think needs to, but after compiling I receive the error which basically corresponds to:

      "variable has private access in Class"

      The class I need to reference is declared in the instance variables at the top of the code.

      Any ideas? :)
        • 1. Re: Accessing a Private Class
          807599
          Some code would help a little.
          • 2. Re: Accessing a Private Class
            800322
            Create a getter method. Or better, think about moving the method that need's the other class's data into the other class so it won't have to ask for information.
            • 3. Re: Accessing a Private Class
              800322
              The class I need to reference is declared in the
              instance variables at the top of the code.
              This description makes absolutely no sense.
              • 4. Re: Accessing a Private Class
                807599
                I am unable to modify the second class that I am trying to access. (read: not allowed to - task set to me by my employer)

                I've just read on using various getter methods. The value I'm trying to access from the second class is an integer.

                I used the code:

                get <classname>.<value>;

                ..and the compiler gives me the error "; expected" and highlights the line above.

                In regards to my comment about the other class being declared at the top of the code, it states this line:

                private<classname> home<classname>;
                Again, apologies if anything I post does not make sense. I can't really post sections of code as my employer is quite anal about getting help rather than studying :-( I've studied for hours on this and still can't get it work work! grr
                • 5. Re: Accessing a Private Class
                  807599
                  I used the code:

                  get <classname>.<value>;

                  ..and the compiler gives me the error ";
                  expected"
                  and highlights the line above.
                  That's because it's not valid Java. It's not even close.

                  I can't really post sections of code as my
                  employer is quite anal about getting help rather than
                  studying :-( I've studied for hours on this and still
                  can't get it work work! grr
                  You're just going to have to do some studying. Read an introductory Java text. If your employer is opposed to learning...then for God's sake quit that job right now!

                  Re: your problem: let me repeat what others have said. You shouldn't be trying to access a private variable. You could use (or create and use) a non-private method that returns the value of that variable, but that's not much better. A redesign is better. If class A needs a value in class B to do its work, then either the value should really be kept in A, or the work should be done by B. That's called "encapsulation", and you probably shouldn't be working as a Java programmer if you don't know what that is.
                  • 6. Re: Accessing a Private Class
                    807599
                    I am unable to modify the second class that I am
                    trying to access. (read: not allowed to - task set to
                    me by my employer)
                    There are good ways to write code. It sounds very much like you're expected to do something in a horribly wrong way.

                    Your employer sounds like a real asswipe. Again I suggest that you quit, preferably in a huff.
                    • 7. Re: Accessing a Private Class
                      807599
                      I can't really post sections of code as my
                      employer is quite anal about getting help rather than
                      studying
                      Then you can't be helped. And your employer is an idiot to boot. Nobody wants to steal your code when you are posting code in the New to Java forum.

                      But if you can't post your code you can't be helped. It's that simple. We are not mindreaders somehow in tune with your compiler through the special ESPP.
                      • 8. Re: Accessing a Private Class
                        807599
                        I am unable to modify the second class that I am
                        trying to access. (read: not allowed to - task set to
                        me by my employer)

                        I've just read on using various getter methods. The
                        value I'm trying to access from the second class is
                        an integer.

                        I used the code:

                        get <classname>.<value>;

                        ..and the compiler gives me the error ";
                        expected"
                        and highlights the line above.

                        In regards to my comment about the other class being
                        declared at the top of the code, it states this
                        line:

                        private<classname> home<classname>;
                        Again, apologies if anything I post does not make
                        sense. I can't really post sections of code as my
                        employer is quite anal about getting help rather than
                        studying :-( I've studied for hours on this and still
                        can't get it work work! grr
                        First of all, I don't think
                        get 
                        is a Java keyword.

                        Second of all, by
                        <classname>.<value>;
                        do you mean you are trying to access a class' static variable? Because otherwise you use an object variable's name, not the classname.