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!!

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. :)

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.

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'?

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. :S what you reckon?

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:

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

   public Calculations getCalculator() {
      return calc;
   }
} //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!!
   Parameters pram = new Parameters();
   Calculators calc2 = new Calculators();
   calc2 = pram.getCalculator(); 
}

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:

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

   public Calculations getCalculator() {
      return calc;
   }
} //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!!
   Parameters pram = new Parameters();
   Calculators calc2 = new Calculators();
   calc2 = pram.getCalculator(); 
}

Still doesn't work, even with the modifications given above. I still work on it though, must be some way round it!!

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?

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?

Actually you dont even need to assign (I was getting an error too.) This does compile:

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 = pram.getCalculator(); 
    // or you could even say (since calc is public)
    Calculations calc3 = pram.calc; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.