Gibbo
Gibbo

Reputation: 660

multiple instance can't use the same variable, variable not changing

I am not quite sure what is wrong with this, I am sure it has to do with the scope. Yesterday I had an issue where a field would initialize itself back to zero due to me recalling the method more than once, making a class field fixed this as it retained it's value regardless of how many time any method is called.

Now I have the opposite problem, I need the field to reset because another object needs to use it(is this possible/bad practice?)

Here is the code:

public class TestDigitalCamera {

    static String brand;
    static double megaPixels;
    static double price;

    //create 2 camera instances with the values of the variables tied to the arguments.
    static DigitalCamera camera = new DigitalCamera(brand, megaPixels);
    static DigitalCamera camera2 = new DigitalCamera(brand, megaPixels);


    public static void main(String[] args) {

        //no idea what this technique is called, need to look back but I know what it does
        //I could use a for loop and reuse the same object over and over(would that even work anyway?) but the task says
        //that i require 4 instances, ofc I am just working with 2 atm for simplicity
        camera = getInformation(camera);
        displayInfo();
        camera2 = getInformation(camera2);
        displayInfo();


    }

    //it basically runs this for the camera instance...right? lol
    public static DigitalCamera getInformation(DigitalCamera dc){
        Scanner userInput = new Scanner(System.in);

        //self explanatory
        System.out.println("Enter brand: ");
        brand = userInput.next();
        System.out.println("Enter Mega Pixels: ");
        megaPixels = userInput.nextDouble();

        //I have another class setup with getters/setters for this, which get used in the next method
        dc.setBrand(brand);
        dc.setMegaPixels(megaPixels);
        return dc;      

    }

    public static void displayInfo(){

        //users the getters to pull the values
        //the price is calculated using an if statement
        System.out.println("Brand: " + camera.getBrand() + "\n"
                + "Megapixels : " + camera.getMegaPixels() + "\n"
                        + "Price : $" + camera.getPrice() + "\n");
    }

}

Is this due to the scope? the variable is use-able for any and all objects but it may only be used for 1? What's the best way to approach this?

Upvotes: 1

Views: 267

Answers (3)

Nu2Overflow
Nu2Overflow

Reputation: 699

I would write your class this way

public class TestDigitalCamera {

public static test_display(){
    DigitalCamera camera = getCamera();
    System.out.println("Brand: " + camera.getBrand() + "\n"
            + "Megapixels : " + camera.getMegaPixels() + "\n"
            + "Price : $" + camera.getPrice() + "\n");

}

public static getCamera(){
    Scanner userInput = new Scanner(System.in);

    //self explanatory
    System.out.println("Enter brand: ");
    brand = userInput.next();
    System.out.println("Enter Mega Pixels: ");
    megaPixels = userInput.nextDouble();

    //I have another class setup with getters/setters for this, which get used in the next method
    DigitalCamera dc = new DigitalCamera();
    dc.setBrand(brand);
    dc.setMegaPixels(megaPixels);
    return dc;
}

public static void main(String[] args) {
    test_displainfo();
}

}

Upvotes: 0

jeroen_de_schutter
jeroen_de_schutter

Reputation: 1883

You should refactor your code like this :

public static void displayInfo(DigitalCamera camera){

    //users the getters to pull the values
    //the price is calculated using an if statement
    System.out.println("Brand: " + camera.getBrand() + "\n"
            + "Megapixels : " + camera.getMegaPixels() + "\n"
                    + "Price : $" + camera.getPrice() + "\n");
}

And then in the main method :

public static void main(String[] args) {

    camera = getInformation(camera);
    displayInfo(camera);
    camera2 = getInformation(camera2);
    displayInfo(camera2);


}

If you declare your two DigitalCamera instances in the main method like this

DigitalCamera camera = getInformation(camera);

then there is no need for separate static variables.

I would stronlgy recommend you to read the part about Class and Instance members in the Java Tutorial : http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Upvotes: 0

anubhava
anubhava

Reputation: 785581

You have this code:

camera = getInformation(camera);
displayInfo();
camera2 = getInformation(camera2);
displayInfo();

Here your method displayInfo() is not really taking any argument and printing information from camera object both the times it is called. Even though you are getting reference of camera2 object in second call of getInformation you aren't really printing it.

You can declare displayInfo like this:

public static void displayInfo(DigitalCamera camera) {
    //users the getters to pull the values
    //the price is calculated using an if statement
    System.out.println("Brand: " + camera.getBrand() + "\n"
         + "Megapixels : " + camera.getMegaPixels() + "\n"
         + "Price : $" + camera.getPrice() + "\n");
}

Upvotes: 2

Related Questions