Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why can I not create related instances???? 1

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
hi.
My problem is that I create a number of instances, they all have completely valid methods to assign instance to instance.
When I call these methods in one long method, it works, and all the instances become related etc, but when I call seperate methods from main, each to do some part of the assignmnt, it throws up methods about not being able to resolve symbol (can't assign the instances to each other).

----------------------HotelDemo.java-----------------------
public class HotelDemo
{
public void runIt()
{
//public void createAddresses()
//{
Address a1 = new Address();
Address a2 = new Address();
Address a3 = new Address();
Address a4 = new Address();

Address a5 = new Address();
a5.setHouseNo(101);
a5.setRoadName("Oxford Street");
a5.setTown("London");
a5.setPostCode("W1 3EB");

Address a6 = new Address();
a1.setHouseNo(999);
a1.setRoadName("Hope Street");
a1.setTown("Aberystwyth");
a1.setPostCode("CR55 6TG");
//}

//public void createPeople()
//{
Person p1 = new Person("Joe Wardell");
p1.setAge(18);
a1.setHouseNo(666);
a1.setRoadName("Sunset Blvd");
a1.setTown("New York");
a1.setPostCode("RH19 2EB");
p1.setAddress(a1);

Person p2 = new Person("Ben Price");
p2.setAge(10);
a2.setHouseNo(7);
a2.setRoadName("Smellville");
a2.setTown("New Dork");
a2.setPostCode("TN22 3NW");
p2.setAddress(a2);

Person p3 = new Person("Swanny");
p3.setAge(25);
a3.setHouseNo(321);
a3.setRoadName("Side-burnsville");
a3.setTown("Bananaramaland");
a3.setPostCode("SY67 1NH");
p3.setAddress(a3);

Person p4 = new Person("Abi Starling");
p4.setAge(17);
a4.setHouseNo(26);
a4.setRoadName("Church Lane");
a4.setTown("East Grinstead");
a4.setPostCode("RH19 4EB");
p4.setAddress(a4);
//}
//public void createHotels()
//{
Hotel h1 = new Hotel("Hilton");
h1.setManager(p1);
h1.setReceptionist(p2);
h1.setLocation(a5);

Hotel h2 = new Hotel("Bel-Air view");
h2.setManager(p3);
h2.setReceptionist(p4);
h2.setLocation(a6);
//}
}

public static void main(String[] args)
{
HotelDemo hd1 = new HotelDemo();
hd1.runIt();
//hd1.createAddresses();
//hd1.createHotels();
//hd1.createPeople();
}
}
---------------------------------------------------------
If anyone can explain to me why this happens, then I'd be very grateful! we are all of us living in the gutter.
But some of us are looking at the stars.
 
Hi oxymoron,
Looking at the code you have posted, adding in the commented methods, there should be no problem. Post the error message, that might help us out more.
Happy Birthday to me [bdaycandle]
MarsChelios
 
I think this is probably a scope issue. Since your intially declaring each of your variables inside the methods, other methods cannot access them, in fact as soon as the method has finished executing, your variables are flagged for garbage collection.
Code:
    public void createAddresses()
        {
        Address a1 = new Address(); //memory referenced here
        .
        .
        .
        } //memory dereferenced here

    public void createPeople()
    {
        Person p1 = new Person("Joe Wardell");
        p1.setAge(18);
        a1.setHouseNo(666);  // a1 no longer exists, error
        .
        .
        .
    }

Using a simple example to show the scope issue:
Code:
public class myClass{
	private integer a;	//declaration of a

	public myClass(){
		a = 5;		//a is set to 5
		System.out.println("A = " + a);		//outputs 5
		myMethod();
		System.out.println("A = " + a);		//outputs 5
		secondMethod();
		System.out.println("A = " + a);		//outputs 15
	}

	private void myMethod(){
		private integer a;  //declaration of a
		a = 10;		//a is set to 10
		System.out.println("A = " + a);		//outputs 10
	}

	private void secondMethod(){
		a = 15;
		System.out.println("A = " + a);		//outputs 15
	}
}
Now the idea here is variable scope. The first declaration of the variable a occurs inside the class, but not inside a specific method, this means anyone inside that clkass can access the variable.
First we set it to 5 inside the constructor. It prints out 5.
Then we call the method myMethod. Now what happens here is that it declares a new variable named a. When this variable is declared it is given the scope of the method, so anyone in that method can access the value of a (we set it to 10), but this variable has no meaning outside the method as we can see when we return to the constructor and it outputs 5, which is the only variable a it has access to.
Then we call the secondMethod and it sets the variable a = 15. The reason this value still exists when it returns to the constructor is because the class was setting the value to the global a.

Basically the premise is that when you have multiple layers in an object, the lowest layers can always access the variables declared in higher layers, but not vice versa.

Using your code we can set up a higher level variable that will not be thrown away at the end of the method run. Inthis case I would suggest some sort of collection so that you don't have to hardcode variables names (and therefore limit yourself):
Code:
//this declaration will declare the array for the life of the object, any method in the class will be able to access it
private Address addressList[10]

    public void createAddresses()
        {
        addressList[1] = new Address(); //object referenced in collection instantiated
        .
        .
        .
        }

    public void createPeople()
    {
        Person p1 = new Person("Joe Wardell");
        p1.setAge(18);
        addressList[1].setHouseNo(666); //addressList[1] still exists as it has class wide scope
        .
        .
        .
    }


Hope that helps,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
I guess it's my fault not to have said, but I thought with object orientated programming, it was 'without saying'.
All the instances I create originate in their own seperate class; Person.java, Address.java etc

Why would the mthods not work now????
the error messages I get are:
-------------------------------------------------
C:\Java\CompSci\Assignment5>javac HotelDem
HotelDemo.java:26: cannot resolve symbol
symbol : variable p1
location: class HotelDemo
h1.setManager(p1);
^
HotelDemo.java:27: cannot resolve symbol
symbol : variable p2
location: class HotelDemo
h1.setReceptionist(p2);
^
HotelDemo.java:28: cannot resolve symbol
symbol : variable a5
location: class HotelDemo
h1.setLocation(a5);
^
...etc
------------------------------------------------------
we are all of us living in the gutter.
But some of us are looking at the stars.
 
The reason you are getting these errors is because of what Tarwn said, good job by the way, Tarwn; you are trying to access the instances
Code:
p1
,
Code:
p2
,
Code:
a5
, etc... out of scope.
You can't use a reference to an object unless you define it in a scope block or above a scope block while in the same method/constructor. In this case, you would have to make the
Code:
Person
and
Code:
Address
instances class instances to use them in
Code:
createHotels ()
and
Code:
createPeople ()
. Like this:
Code:
  Address [] addresses = new Address [6];
    ...
  public void createHotels () {
    ...
    h1.setLocation (addresses  [4]);
  }
The error
Code:
cannot resolve symbol
means that the variable is not defined in the current scope, in this case, in the methods
Code:
createHotels ()
and
Code:
createPeople ()
.

Happy Birthday to me, [bdaycandle]
MarsChelios
 
Happy Birthday!

hb2.gif
biggringift.gif


ola.gif

(We're all waiting for the cake)


Well, and the free beer
beerpassout.gif


-Tarwn

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top