944,049 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2728
  • Java RSS
Nov 22nd, 2004
0

problems accessing class instances

Expand Post »
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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
matt_5104 is offline Offline
15 posts
since Nov 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MrScruff is offline Offline
89 posts
since Nov 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
matt_5104 is offline Offline
15 posts
since Nov 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

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'?
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MrScruff is offline Offline
89 posts
since Nov 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

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:
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

Quote 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:
Java Syntax (Toggle Plain Text)
  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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
matt_5104 is offline Offline
15 posts
since Nov 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

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?
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

Quote 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
matt_5104 is offline Offline
15 posts
since Nov 2004
Nov 22nd, 2004
0

Re: problems accessing class instances

Actually you dont even need to assign (I was getting an error too.) This does compile:
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to make Java and C++ sockets play nice
Next Thread in Java Forum Timeline: IO and Embedded SQL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC