Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Accessability/Visability Private

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Thanked 0 Times in 0 Posts

    Default Accessability/Visability Private

    This is part of a assignment, I was instructed to make a variable in a class 'A' private. Class 'B' extends class 'A' and class B uses the variable in class A that I declared private. Now I am going to receive an error message until I deal with the usage of this variable.

    How can a class B object get access to this private variable of class A without changing the accessibility/visibility of the variable?

    Here is a snippet of the code, the variable is "text"

    tokenizer = new StringTokenizer(text, " ");

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Accessability/Visability Private

    Are you allowed to modify class A? If so, I would add what is known as a "getter" method. In essense, it simply returns the value of the variable you want.
    public class A
         private int num;
         // the getter method
         public int getNum()
              return this.num;

Similar Threads

  1. Convert string to private and public key
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 5th, 2010, 08:37 AM
  2. private method , loop , switch , help?
    By wolfgar in forum Loops & Control Statements
    Replies: 9
    Last Post: November 8th, 2009, 11:04 PM
  3. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM