problems accessing class instances

Reply

Join Date: Nov 2004
Posts: 15
Reputation: matt_5104 is an unknown quantity at this point 
Solved Threads: 0
matt_5104 matt_5104 is offline Offline
Newbie Poster

problems accessing class instances

 
0
  #1
Nov 22nd, 2004
I'm having a problem whereby I create an instance of a class called Calculations.class in one of my main classes, but cannot access that instance from any other class. Here is the code

public class Parameters{
//code//
Calculations calc = new Calculations();
//some calculations done using variables in the calculations class and new data is saved//
//end of Parameters.class//
}

public class Intake{
// I now want to work with the instance "calc" created above so I can use the variables that were set in Parameters.class, but I don't know how to reference this instance!! //

Any help will be greatly appreciated, thank you!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Re: problems accessing class instances

 
0
  #2
Nov 22nd, 2004
You could make a method in your parameters class called getCalculator, which simply returns the calculator -

public Calculator getCalculator()
{
return calc;
}

Then create an instance of parameters in intake and call the getCalculator method upon it.

Parameters pram = new Parameters();
Calculators calc2 = new Calculators();
calc2 = pram.getCalculator();

Hopefully that will help.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 15
Reputation: matt_5104 is an unknown quantity at this point 
Solved Threads: 0
matt_5104 matt_5104 is offline Offline
Newbie Poster

Re: problems accessing class instances

 
0
  #3
Nov 22nd, 2004
Originally Posted by MrScruff
You could make a method in your parameters class called getCalculator, which simply returns the calculator -

public Calculator getCalculator()
{
return calc;
}

Then create an instance of parameters in intake and call the getCalculator method upon it.

Parameters pram = new Parameters();
Calculators calc2 = new Calculators();
calc2 = pram.getCalculator();

Hopefully that will help.
Hi, it almost helps, but when I try and run the program, it comes up with:

Intake.java [189:1] <identifier> expected
calc = pram.getCalculator();
^
1 error
Errors compiling Intake.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: problems accessing class instances

 
0
  #4
Nov 22nd, 2004
public Calculator getCalculator()
{
return calc;
}

You never declare what 'calc' is.

public Calculator getCalculator()
{
Calculator calc;
return calc;
}

However I would assume that you would assign something to 'calc'?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Re: problems accessing class instances

 
0
  #5
Nov 22nd, 2004
it would have to be the calculator created in parameters so

public Calculator getCalculator()
{
Calculator calc1;
calc1 = calc;
return calc1;
}

hows that? but then if its been declared public at the top you should just be able to return calc. what you reckon?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: problems accessing class instances

 
0
  #6
Nov 22nd, 2004
I think what you have is an issue of scope.

Looking at the above, I think you placed the getCalculator method in the wrong class. It should go in the Parameters Class.

So you would have the following:
  1. public class Parameters{
  2. Calculations calc = new Calculations();
  3. //some calculations done using variables in the calculations class
  4. //and new data is saved
  5.  
  6. public Calculations getCalculator() {
  7. return calc;
  8. }
  9. } //end of Parameters.class
  10.  
  11. public class Intake{
  12. // I now want to work with the instance "calc" created above so I can
  13. // use the variables that were set in Parameters.class, but I don't know
  14. // how to reference this instance!!
  15. Parameters pram = new Parameters();
  16. Calculators calc2 = new Calculators();
  17. calc2 = pram.getCalculator();
  18. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 15
Reputation: matt_5104 is an unknown quantity at this point 
Solved Threads: 0
matt_5104 matt_5104 is offline Offline
Newbie Poster

Re: problems accessing class instances

 
0
  #7
Nov 22nd, 2004
Originally Posted by jerbo
I think what you have is an issue of scope.

Looking at the above, I think you placed the getCalculator method in the wrong class. It should go in the Parameters Class.

So you would have the following:
  1. public class Parameters{
  2. Calculations calc = new Calculations();
  3. //some calculations done using variables in the calculations class
  4. //and new data is saved
  5.  
  6. public Calculations getCalculator() {
  7. return calc;
  8. }
  9. } //end of Parameters.class
  10.  
  11. public class Intake{
  12. // I now want to work with the instance "calc" created above so I can
  13. // use the variables that were set in Parameters.class, but I don't know
  14. // how to reference this instance!!
  15. Parameters pram = new Parameters();
  16. Calculators calc2 = new Calculators();
  17. calc2 = pram.getCalculator();
  18. }
Still doesn't work, even with the modifications given above. I still work on it though, must be some way round it!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: problems accessing class instances

 
0
  #8
Nov 22nd, 2004
Opps, I mispelled, Calculations

The second class should be:
public class Intake{
   // I now want to work with the instance "calc" created above so I can 
   // use the variables that were set in Parameters.class, but I don't know
   // how to reference this instance!!
   Parameters pram = new Parameters();
   Calculations calc2 = new Calculations();
   calc2 = pram.getCalculator(); 
}

Also I suspect you have Calculations defined somewhere?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 15
Reputation: matt_5104 is an unknown quantity at this point 
Solved Threads: 0
matt_5104 matt_5104 is offline Offline
Newbie Poster

Re: problems accessing class instances

 
0
  #9
Nov 22nd, 2004
Originally Posted by jerbo
Opps, I mispelled, Calculations

The second class should be:
public class Intake{
   // I now want to work with the instance "calc" created above so I can 
   // use the variables that were set in Parameters.class, but I don't know
   // how to reference this instance!!
   Parameters pram = new Parameters();
   Calculations calc2 = new Calculations();
   calc2 = pram.getCalculator(); 
}

Also I suspect you have Calculations defined somewhere?
Yes, calculations is defined elsewhere. However, when I add the code you provided me with(very grateful!!), the compiler returns with an error saying that an identifier is missing before the line of code:

calc2 = pram.getCalculator();

What do I need to do now?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: problems accessing class instances

 
0
  #10
Nov 22nd, 2004
Actually you dont even need to assign (I was getting an error too.) This does compile:
  1. public class Intake{
  2. // I now want to work with the instance "calc" created above so I can
  3. // use the variables that were set in Parameters.class, but I don't know
  4. // how to reference this instance!!
  5. Parameters pram = new Parameters();
  6. Calculations calc2 = pram.getCalculator();
  7. // or you could even say (since calc is public)
  8. Calculations calc3 = pram.calc;
  9. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC