Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Understanding the syntax for static and instance methods

    I understand that a static method or variable is associated with the class as a whole rather than with specific instances of a class. If there are multiple objects created from multiple instantiations of a class, then a static variable or method cannot access a specific object. Since I understand that much, the purpose of this exercise is to deepen my understanding of the syntax for using static and instance fields/methods. With that being said, here's my code:

    MyObject.java:
    Java Code:
    package modifiers.business;
    
    import java.text.NumberFormat;
    
    public class MyObject {
    	private int ID;
    	private String name;
    	private String color;
    	private double price;
    	
    	public MyObject(){
    		this(0, "", "", 0.0);
    	}
    	
    	public MyObject(int ID, String name, String color, double price){
    		this.ID = ID;
    		this.name = name;
    		this.color = color;
    		this.price = price;
    	}
    	/*
    	public void setID(int currID){
    		ID = currID;
    	}
    	public int getID(){
    		return ID;
    	}
    	*/
    	public void setID(int ID){
    		this.ID = ID;
    	}
    	public int getID(){
    		return ID;
    	}
    	public void setName(String name){
    		this.name = name;
    	}
    	public String getName(){
    		return name;
    	}
    	public void setColor(String color){
    		this.color = color;
    	}
    	public String getColor(){
    		return color;
    	}
    	public void setPrice(double price){
    		this.price = price;
    	}
    	public double getPrice(){
    		return price;
    	}
    	public String getPriceFormatted(){
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		String priceFormatted = currency.format(price);
    		return priceFormatted;
    	}
    }
    MyObjectDB.java:
    Java Code:
    package modifiers.db;
    
    import modifiers.business.MyObject;
    
    public class MyObjectDB {
    	public MyObject getMyObject(int objectID){//non-static
    		//public static MyObject getMyObject(int objectID){
    			MyObject obj = new MyObject();
    			
    			if(objectID == 0){
    				obj.setID(objectID);
    				obj.setName("Lizard");
    				obj.setColor("Green");
    				obj.setPrice(5.00);
    			}else if(objectID == 1){
    				obj.setID(objectID);
    				obj.setName("Grizzly");
    				obj.setColor("Brown");
    				obj.setPrice(600.00);
    			}else if(objectID == 2){
    				obj.setID(objectID);
    				obj.setName("Zebra");
    				obj.setColor("BlackAndWhiteStripes");
    				obj.setPrice(40.00);
    			}else if(objectID == 3){
    				obj.setID(objectID);
    				obj.setName("Squirrel");
    				obj.setColor("Red");
    				obj.setPrice(3.00);
    			}else if(objectID == 4){
    				obj.setID(objectID);
    				obj.setName("Pig");
    				obj.setColor("Pink");
    				obj.setPrice(98.00);
    			}else{
    				System.out.println("Invalid ID.");
    			}
    			return obj;
    	}
    	public static void additionalCreature(){
    		int creatureID = 5;
    		String name = "AlienJellyFish";
    		String color = "NeonGreen";
    		double price = 400.00;		
    	}
    }
    MyObjectApp.java:
    Java Code:
    package modifiers.ui;
    import java.util.Scanner;
    import modifiers.db.*;
    import modifiers.business.*;
    
    public class MyObjectApp {
    	
    	//use a static method to access a specific instance field using an instance reference:
    	public static MyObject.color meWantBrownCreature(){//search the getMyCreature method for a creature
    		//that is brown.
    		
    		//get the MyObject object:
    		String colorIWant = "Brown";
    		MyObjectDB db = new MyObjectDB();
    		MyObject obj = db.getMyObject(obj.objectID));//error: objectID cannot be resolved or is not a field
    		if(obj.setColor.equalsIgnoreCase("Brown"){//error: setColor cannot be resolved or is not a field
    			return obj.getName();
    		}
    	}
    	
    	//use an instance method to access a static member:
    	public void getAdditionalCreature(){
    		MyObject obj = new MyObject();
    		obj.getID() = additonalCreature.creatureID;//error: The left-hand side of an assignment must be a variable
    		obj.getName() = additionalCreature.name;
    		obj.getColor() = additionalCreature.color;
    		obj.getPrice() = additionalCreature.price;
    	}	
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Understanding the syntax for static and instance methods

    Please don't insert your questions into the code as it means we have to trawl through the code to actually find out what it is you are asking.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Understanding the syntax for static and instance methods

    And please keep your examples short. All you need to do was have color and id and only two of them to start. This eliminates
    a lot of unnecessary code (if blocks, setters and getters, etc) but can still illustrate the problem.

    You are using methods as variables and variables as methods!! See MyObjectApp.



    Java Code:
    public int foo() {
    }
    // The following syntax is wrong.
    
    foo.something = 1; // foo is a method and should be called like one
    
    foo() = something; // methods may not be the targets of an assignment.
    Regards,
    Jim
    Last edited by jim829; 08-26-2016 at 02:53 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Understanding the syntax for static and instance methods

    Quote Originally Posted by jim829 View Post
    And please keep your examples short.
    As you wish. Here is some shorter code:

    MyObject.java:
    Java Code:
    package modifiers.business;
    
    import java.text.NumberFormat;
    
    public class MyObject {
    	private int ID;
    	private String name;
    	private String color;
    	
    	public MyObject(){
    		this(0, "", "");
    	}
    	
    	public MyObject(int ID, String name, String color){
    		this.ID = ID;
    		this.name = name;
    		this.color = color;
    	}
    
    	public void setID(int ID){
    		this.ID = ID;
    	}
    	public int getID(){
    		return ID;
    	}
    	public void setName(String name){
    		this.name = name;
    	}
    	public String getName(){
    		return name;
    	}
    	public void setColor(String color){
    		this.color = color;
    	}
    	public String getColor(){
    		return color;
    	}	
    }
    MyObjectDB.java:
    Java Code:
    package modifiers.db;
    
    import modifiers.business.MyObject;
    
    public class MyObjectDB {
    	public MyObject getMyObject(int objectID){//non-static
    		//public static MyObject getMyObject(int objectID){
    			MyObject obj = new MyObject();
    			
    			if(objectID == 0){
    				obj.setID(objectID);
    				obj.setName("Lizard");
    				obj.setColor("Green");
    			}else if(objectID == 1){
    				obj.setID(objectID);
    				obj.setName("Grizzly");
    				obj.setColor("Brown");
    			}else{
    				System.out.println("Invalid ID.");
    			}
    			return obj;
    	}
    	
    	public static void getAdditionalCreatureNameField(){
    		String nameField = "JellyFish";
    	}
    	public static String getAdditionalCreatureColorField(){
    		String color = "NeonGreen";
    		return color;
    	}	
    }
    MyObjectApp.java:
    Java Code:
    package modifiers.ui;
    import java.util.Scanner;
    import modifiers.db.*;
    import modifiers.business.*;
    
    public class MyObjectApp {
    	
    	//use a static method to access a specific instance field using an instance reference:
    	public static MyObject meWantBrownCreature(){
    		MyObjectDB db = new MyObjectDB();
    		MyObject obj = db.getMyObject(obj.setColor());
    		
    		if(obj.setColor.equalsIgnoreCase("Brown")){
    			System.out.println("I am a " + obj.getName() + ", and I'm " + obj.getColor());
    			System.out.println("My ID number is " + obj.getID());
    			return obj;
    		}
    	}
    	
    	//use an instance method to access a static member:
    	public MyObject getAdditionalCreature(){
    		MyObject obj = new MyObject();
    		
    		obj.setID(5);
    		obj.setName(MyObjectDB.getAdditionalCreatureNameField().nameField);
    		obj.setColor(MyObjectDB.getAdditionalCreatureColorField());
    		
    		System.out.println("I am a " + obj.getName() + ", and I'm " + obj.getColor());
    		System.out.println("My ID number is " + obj.getID());
    		return obj;
    	}
    	
    	public static void main(String args[]){
    		MyObject obj = getAdditionalCreature();
    		
    		MyObject obj = MyObjectApp.meWantBrownCreature();
    	}
    }
    The purpose of the meWantBrownCreature static method is to detect and select the defined object from the getMyObject instance method in MyObjectDB.java whose String color field is "Brown". Of course, the way the code is set up right now, I'm not sure how to detect an object in the getMyObject method by it's color instead of its objectID. Is there a way to do that?

    MyObjectApp.java errors:
    Line 11: "The method setColor(String) in the type MyObject is not applicable for the arguments ()"
    Line 13: "setColor cannot be resolved or is not a field"

    --------------------

    The purpose of the getAdditionalCreature instance method is to create a new creature (that's not defined in the getMyObject method) by instantiating the 3 fields in different ways:

    1. simply set he ID to an arbitrary value

    2. set the name field by directly accessing the String variable defined in the static void getAdditionalCreatureNameField method (if that's possible)

    3. set the color field by calling the static getAdditionalCreatureColorField method

    MyObjectApp.java errors:
    Line 25: "nameField cannot be resolved or is not a field"

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Understanding the syntax for static and instance methods

    OK. I will simply point out the syntax problems (and a few design errors).

    In class MyObjectDB

    Java Code:
        
    public static void getAdditionalCreatureNameField() {
          String nameField = "JellyFish";
    }
    This is a getter but it is declared void. Getters get something. You need to return
    a string (e.g. return "JellyFish";)

    In class MyObjectApp

    Line 11. Looks like you are using Obj.setColor() to get a color.
    You probably meant Obj.getColor(). However, once you change
    it, it won't work because db.getMyObject(Obj.getColor()) won't work
    because getMyObject() expect an objectID which is an int, and not a color.
    To solve this you can overload the method to accept a color. That means
    create another getMyObject() method that takes a string.

    Line 13. setColor is a method. You are using it as a field.
    Maybe you meant obj.getColor().equalsIgnoreCase("Brown")

    Line 25. Improper use of getter (see above). Should look more
    like Line 26 when you fix the above getter().

    Lines 33 to 37. You have the following code:

    Java Code:
     
    public static void main(String args[]){
    
    // You can't do the following because getAdditionalCreature() is an
    // instance method and main is a static context.
         MyObject obj = getAdditionalCreature();   
    
    // The following call is okay.
         MyObject obj = MyObjectApp.meWantBrownCreature();
    
    }
    As I said in another post, do something like the following:

    Java Code:
    public static void main(String[] args) {
        new MyObjectApp().start();
    }
    public void start() { // note the use of obj1 and obj2.  Makes compiler error go away.
         MyObject obj1 = getAdditionalCreature();
         MyObject obj2 = MyObjectApp.meWantBrownCreature();
    }
    Regards,
    Jim
    Last edited by jim829; 08-27-2016 at 02:22 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Understanding the syntax for static and instance methods

    Quote Originally Posted by jim829 View Post
    This is a getter but it is declared void. Getters get something. You need to return
    a string (e.g. return "JellyFish";)
    I'm guessing that means you can never access a field from a static method unless it's returned? Well that's disappointing

    I'm not holding my breath, but is it possible to return more than one field from one method if they're the same datatype? I'm asking because the name and color fields are both String datatypes. So can the following method be modified to do that:
    Java Code:
    public static String getAdditionalCreatureColorField(){
    		String color = "NeonGreen";
    		String name = "JellyFish";
    		return color, name;
    	}
    Also, despite writing a valid overload method in MyObjectDB.java:
    Java Code:
    //overload the getMyObject method that takes a color string instead of objectID:
    	public MyObject getMyObject(String objColor){
    		MyObject obj = new MyObject();
    		
    		if(objColor.equalsIgnoreCase("Green")){
    			obj.setID(0);
    			obj.setName("Lizard");
    			obj.setColor("Green");
    		}else if(objColor.equalsIgnoreCase("Brown")){
    			obj.setID(1);
    			obj.setName("Grizzly");
    			obj.setColor("Brown");
    		}else{
    			System.out.println("Invalid color.");
    		}
    		return obj;
    	}
    I'm still having problems with the meWantBrownCreature method. The following if statement:
    Java Code:
    if(obj.getColor.equalsIgnoreCase("Brown")){			
    				return obj;
    			}
    gave the error "getColor cannot be resolved or is not a field", so I tried this instead:
    Java Code:
    //use a static method to access a specific instance field using an instance reference:
    		public static MyObject meWantBrownCreature(){
    			MyObjectDB db = new MyObjectDB();
    			MyObject obj = db.getMyObject(obj.getColor());
    			
    			String matchWithBrownColor = obj.getColor();
    			if(matchWithBrownColor.equalsIgnoreCase("Brown")){
    			//if(obj.getColor.equalsIgnoreCase("Brown")){			
    				return obj;
    			}
    		}
    But the errors were:
    Line 2: "This method must return a result of type MyObject"
    Line 4: "The local variable obj may not have been initialized"

    I'm not sure why the error on line 2 occurs. The return statement in the curly braces of the if statement returns obj, which is a MyObject return type.

    Your start method in the main method works great by the way. Thanks for teaching me that technique

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Understanding the syntax for static and instance methods

    Quote Originally Posted by Sam_JavaTheHut5580 View Post
    I'm guessing that means you can never access a field from a static method unless it's returned? Well that's disappointing
    All declared values inside a method, static or otherwise, are local to that method. If a method is doing something with a static or instance field, you can still
    retrieve the field either directly (if properly qualified) or via a getter.
    I'm not holding my breath, but is it possible to return more than one field from one method if they're the same datatype? I'm asking because the name and color fields are both String datatypes. So can the following method be modified to do that:
    Java Code:
    public static String getAdditionalCreatureColorField(){
            String color = "NeonGreen";
            String name = "JellyFish";
            return color, name;
        }
    No. But you can always declare a small class to be used in passing around variables. Just populate an instance of the class
    within the method and return the instance.


    I'm still having problems with the meWantBrownCreature method. The following if statement:
    Java Code:
    if(obj.getColor.equalsIgnoreCase("Brown")){            
                    return obj;
                }

    because the above syntax is incorrect since getColor is a method not a reference. It should be like this.
    Java Code:
    if(obj.getColor().equalsIgnoreCase("Brown")){            
                    return obj;
     }
    gave the error "getColor cannot be resolved or is not a field", so I tried this instead:
    Java Code:
    //use a static method to access a specific instance field using an instance reference:
            public static MyObject meWantBrownCreature(){
                MyObjectDB db = new MyObjectDB();
                MyObject obj = db.getMyObject(obj.getColor());
                
                String matchWithBrownColor = obj.getColor();
                if(matchWithBrownColor.equalsIgnoreCase("Brown")){
                //if(obj.getColor.equalsIgnoreCase("Brown")){            
                    return obj;
                }
            }
    But the errors were:
    Line 2: "This method must return a result of type MyObject"
    Line 4: "The local variable obj may not have been initialized"

    I'm not sure why the error on line 2 occurs. The return statement in the curly braces of the if statement returns obj, which is a MyObject return type.
    The error on line 4 occurs because of the following:
    Java Code:
     MyObject obj = db.getMyObject(obj.getColor());
    where do you initialize obj on the right side of the = sign?

    The error on line 2 occurs because you only return an obj if the condition succeeds. What
    do you return if it doesn't succeed? It still must be an object. In this case, null is sometimes
    used to indicate failure upon return. But you must check the return value before using it.

    Your start method in the main method works great by the way. Thanks for teaching me that technique
    You're Welcome!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Understanding the syntax for static and instance methods

    For the method in MyObjectApp.java:
    Java Code:
    //use a static method to access a specific instance field using an instance reference:
    		public static MyObject meWantBrownCreature(){
    			MyObjectDB db = new MyObjectDB();
    			MyObject obj = db.getMyObject(objColor);
    			
    			if(obj.getColor().equalsIgnoreCase("Brown")){			
    				return obj;
    			}
    			else{
    				System.out.println("Sorry, no brown creature was found.");
    				return obj = null;
    			}
    		}
    I'm getting an error on line 4: "objColor cannot be resolved to a variable".
    Logically, I understand the error, but I don't know how to syntactically correct it.

    And making the method static in MyObjectDB.java didn't help either:
    Java Code:
    //overload the getMyObject method that takes a color string instead of objectID:
    	public MyObject getMyObject(String objColor){
    		MyObject obj = new MyObject();
    		
    		if(objColor.equalsIgnoreCase("Green")){
    			obj.setID(0);
    			obj.setName("Lizard");
    			obj.setColor("Green");
    		}else if(objColor.equalsIgnoreCase("Brown")){
    			obj.setID(1);
    			obj.setName("Grizzly");
    			obj.setColor("Brown");
    		}else{
    			System.out.println("Invalid color.");
    		}
    		return obj;
    	}
    So what do I need to do?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Understanding the syntax for static and instance methods

    I'm getting an error on line 4: "objColor cannot be resolved to a variable".
    Logically, I understand the error, but I don't know how to syntactically correct it.
    You need to declare and initialize objColor somewhere appropriate in your code. It must be
    visible to the method in which it is used. You can also simply pass it as an argument to the method.

    And use print statements to ensure that your variables are what you expect them to be.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Understanding the syntax for static and instance methods

    Jim, in post #7 you said:
    Quote Originally Posted by jim829 View Post
    The error on line 2 occurs because you only return an obj if the condition succeeds. What
    do you return if it doesn't succeed? It still must be an object. In this case, null is sometimes
    used to indicate failure upon return. But you must check the return value before using it.
    Accordingly, I tried countering this problem by telling the program to return null if the object feature isn't brown (on line 28 of MyObjectApp.java):

    MyObjectApp.java:
    Java Code:
    package modifiers.ui;
    import java.util.Scanner;
    import modifiers.db.*;
    import modifiers.business.*;
    
    public class MyObjectApp {
    	
    	//use an instance method to access a static member:
    	public MyObject getAdditionalCreature(){
    		MyObject obj = new MyObject();
    		
    		obj.setID(5);
    		obj.setName("JellyFish");
    		obj.setColor(MyObjectDB.getAdditionalCreatureColorField());		
    		return obj;
    	}
    	//use a static method to access a specific instance field using an instance reference:
    		public static MyObject meWantBrownCreature(){
    			String objColor = "";
    			MyObjectDB db = new MyObjectDB();
    			MyObject obj = db.getMyObject(objColor);//loop through the overloaded getMyObject method
    			
    			if(obj.getColor().equalsIgnoreCase("Brown")){			
    				return obj;
    			}
    			else{
    				System.out.println("Sorry, no brown creature was found.");
    				return obj = null;
    			}
    		}
    	
    	public static void main(String args[]){
    		new MyObjectApp().start();
    	}
    	public void start() {
    	     MyObject obj1 = getAdditionalCreature();
    	     System.out.println("I am a " + obj1.getName() + ", and I'm " + obj1.getColor());
    		 System.out.println("My ID number is " + obj1.getID());
    	     
    	     MyObject obj2 = MyObjectApp.meWantBrownCreature();
    	     System.out.println("I am a " + obj2.getName() + ", and I'm " + obj2.getColor());
    		 System.out.println("My ID number is " + obj2.getID());
    	}	
    }
    Method from MyObjectDB.java:
    Java Code:
    //overload the getMyObject method that takes a color string instead of objectID:
    	public MyObject getMyObject(String objColor){
    		MyObject obj = new MyObject();
    		
    		if(objColor.equalsIgnoreCase("Green")){
    			obj.setID(0);
    			obj.setName("Lizard");
    			obj.setColor("Green");
    		}else if(objColor.equalsIgnoreCase("Brown")){
    			obj.setID(1);
    			obj.setName("Grizzly");
    			obj.setColor("Brown");
    		}else{
    			System.out.println("Invalid color.");
    		}
    		return obj;
    	}
    But After I compiled my project, I got this:
    Understanding the syntax for static and instance methods-nullpointer.png

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Understanding the syntax for static and instance methods

    Find the variable with the null value on line 41. Then backtrack in the code to see why the variable with the null value did not have a valid value.
    If null is an expected value in the variable, the code needs to test for null before using the variable to reference anything.
    Last edited by Norm; 09-04-2016 at 10:19 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Join Date
    Mar 2016
    Posts
    93
    Rep Power
    0

    Default Re: Understanding the syntax for static and instance methods

    The meWantBrownCreature method in MyObjectApp.java is now working:
    Java Code:
    	//use a static method to access a specific instance field using an instance reference:
    		public static MyObject meWantBrownCreature(){
    		//public static MyObject colorOfMyCreature(){
    			Scanner color = new Scanner(System.in);
    			System.out.println("What color creature do you want?");
    			String objColor = color.nextLine();
    			MyObjectDB db = new MyObjectDB();
    			MyObject obj = db.getMyObject(objColor);//loop through the overloaded getMyObject method			
    			
    			if((obj != null)&&((obj.getColor().equalsIgnoreCase("Brown"))||
    			(obj.getColor().equalsIgnoreCase("Green")))){
    			//if(objColor.equalsIgnoreCase("Brown")){
    				return obj;
    			}
    			else{
    				System.out.println("Sorry, we don't have any " + objColor + " creatures.");
    				return obj = null;
    			}
    		}
    But how do you make the comparison dynamic:
    Java Code:
    ((obj.getColor().equalsIgnoreCase("Brown"))||(obj.getColor().equalsIgnoreCase("Green")))
    So that it will still detect whether the objColor string that the user entered in matches one of the objColor variables in the getMyObject method:
    Java Code:
    public MyObject getMyObject(String objColor){
    		MyObject obj = new MyObject();
    		
    		if(objColor.equalsIgnoreCase("Green")){
    			obj.setID(0);
    			obj.setName("Lizard");
    			obj.setColor(objColor);
    		}else if(objColor.equalsIgnoreCase("Brown")){
    			obj.setID(1);
    			obj.setName("Grizzly");
    			obj.setColor(objColor);
    		}else{
    			System.out.println("Invalid color.");
    		}
    		return obj;
    	}
    without having to hard code the color string parameters "Brown" or "Green" in the equalsIgnoreCase statements?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Understanding the syntax for static and instance methods

    An idea: Use a Map with String as the key and MyObject as the value.
    Load the Map with the MyObject objects with their colors: brown and green
    Set the passed key to lowercase when accessing the Map.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Understanding the syntax for static and instance methods

    So you need to check if you get a null return value. That means there is some kind of error and you need to handle it
    appropriately. Line 23 does not help the situation at all. You set it to "" and then call getMyObject using that value. What did
    you expect to get when you ask it for a color based on an empty string?

    And this really has nothing to do with my post #7 that you quoted.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Understanding 'static' a little more
    By salmontres in forum New To Java
    Replies: 5
    Last Post: 12-12-2011, 11:37 PM
  3. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  4. Understanding -- new Class() -- syntax.
    By AcousticBruce in forum New To Java
    Replies: 8
    Last Post: 12-14-2010, 12:53 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •