-1

How can I access objects created in one method in other methods?

I want to do this so that I don't have create the same object over and over again every time I create a new method, and want to use that object within it. This is the class I am working on:

import BrandPhone.*;

public class Test {

    public static void main(String[] args) {

        // Creating the object. 
        Phone myPhone = new Phone();

        //Creating another object.
        Color color = myPhone.getColor();
    }
}

If I try and access the object color in another new method like the method initialise seen below, I get an error saying "cannot find symbol":

import BrandPhone.*;

public class Test {

    public static void main(String[] args) {

        // Creating the object. 
        Phone myPhone = new Phone();

        //Creating another object.
        Color color = myPhone.getColor();
    }

    public void initialise() {

        color.setNum(120);    // This gives an error

    }
}

To try and solve this issue, I decided to try and declare the object names as class variables at the beginning of the class, outside of any method. This produces errors saying "non- static variable ('object name') cannot be references from a static context".

import BrandPhone.*;

public class Test {

    Phone myPhone;
    Color color;

    public static void main(String[] args) {

        // Creating the object. 
        myPhone = new Phone();         // Produces error.

        //Creating another object.
        color = myPhone.getColor();    // Produces error. 
    }
    
    public void initialise() {
    
        color.setNum(120);     

    }
}

To attempt to solve this issue, I changed the method so that instead of public static void main(String[] args) {...} I made it public void newObjects() {...}. This produces yet another error when I try to run it, saying "Class "Assignment.Test" does not have a main method."

I'm very new to Java, so I'm not sure how to tackle this- do all classes have to have a main method in the form of public static void main(String[] args) {...}? If so, could I not just start with a main method and leave it empty, and then continue on to make new methods?

Community
  • 1
  • 1
p.luck
  • 646
  • 2
  • 9
  • 34
  • 2
    Possible duplicate of [I want to know the difference between static method and non-static method](http://stackoverflow.com/questions/3903537/i-want-to-know-the-difference-between-static-method-and-non-static-method). I would also read: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Nir Alfasi Mar 28 '17 at 17:48
  • *" I decided to try and declare the object names as class variables at the beginning of the script"* - Java is **not** a *scripting language*. What you call a "script" is a *class (source) file*. Be clear with your wording! Walk through the [tutorials](https://docs.oracle.com/javase/tutorial/) – Timothy Truckle Mar 28 '17 at 17:54
  • 1
    `myphone` is different from `myPhone`. Even if you don't care about capitalization, java does ;) – OH GOD SPIDERS Mar 28 '17 at 18:01
  • @Timothy Truckle I have edited my question accordingly, thanks. – p.luck Mar 28 '17 at 18:41

3 Answers3

0

Im not fully sure what your are trying to do. But color.setNum(120) should work if you do it after you declared and initialised color.

Color color = myPhone.getColor();
color.setNum(120);
Mohammad C
  • 1,321
  • 1
  • 8
  • 12
  • but by doing this, surely I have to declare and initialise this object every time I write a new method? – p.luck Mar 28 '17 at 18:39
0

You are trying to access the color variable out of scope, you can pass it directly to the method (shown below), set it as a static variable or just call setNum in your main() method.

public class Test {

public static void main(String[] args) {

    // Creating the object. 
    Phone myPhone = new Phone();

    //Creating another object.
    Color color = myPhone.getColor();

    initialise(color);
}

public static void initialise(Color colorToSet) {

    colorToSet.setNum(120);

}

}

0

You should also provide your Phone and Color class files, to provide an encapsulated look of your project. However, based on your main; the following will work. If you make a static call from the the main() the other method should also be static if also in the Test class. Instantiate your object, Color, and then make your method call to your setter method; color.setNum(120);

public static void main(String[] args) {

    // Creating the object. 
    Phone myPhone = new Phone();

    //Creating another object.
    Color color = myPhone.getColor();
    color.setNum(120); 
}