English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For Java, I am having a problem.

I have given code (we'll call it Given.class), and I have to write another class that utilizes private variables within Given.class.


Given.class has 3 private variables..

My code defines a new class type which is made up of an array of type Given.

I edited the given code, changing the private variables to public variables and my code works fine... but I am not allowed to do this for the assignment. Can anyone give me some help? I'm not really understanding what I need to do.

2007-03-16 16:20:01 · 2 answers · asked by Sean06 2 in Computers & Internet Programming & Design

Doing so is apparently the object of my assignment though.

I get error "building has private access in Classroom" for my error.

where Classroom = Given.class as mentioned earlier.

Is there seriously no way to do this?

2007-03-16 16:32:14 · update #1

The "Given.class" has many methods with various parameters. Using these, I am pretty sure I can do what is required for the assignment, but I am not sure how I would call on them in this manner.

2007-03-16 16:35:53 · update #2

2 answers

There actually is a way. They are called accessors.

The accessors will provide access to this variable by way of a return statement.


example....

pubic int accessor(){
return variable;

make this method public because you want to call it from the other class.

what this does is allow you to call the method from outside this class and have it give you the value of the variable

if you wanted to store it in a variable in the new class you can do this

set up a new object

GivenClass gc = new gc();

variable = gc.accessor;

2007-03-16 16:46:01 · answer #1 · answered by Mantis 2 · 0 0

you cant access a private member outside of a class. Its the concept of OOPs. For more info on accessing members please visit http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

2007-03-16 16:28:09 · answer #2 · answered by Dark Prince 2 · 0 0

fedest.com, questions and answers