0

I have a class Fruits that consists of certain variables:

public class Fruits {
  public static String abc="something";
}

public class Apple extends Fruits {
  public static String abc="something";
}

Depending on the config it selects whether it should return an instance of Fruits or Apple:

Class Fruits{
  public static Fruits getfruit{
    if(config==3) {
      return new Apple();
    } else {
      return new Fruits();
    }
  }
}

So later I can use Utility.getfruit().abc.

6
  • Sorry, do you want to create a class based on a property/configuration? Commented Jan 20, 2022 at 8:33
  • a Class doesn't have access to instance methods. Instances of the class do. You are trying to call constructors, but you forget the ()'s, like: return new Procedures(); It's also String, not string, and to clarify, your classes don't have instance variables Commented Jan 20, 2022 at 8:35
  • 2
    @SSinha 1500 variables (from the other comments, information is missing in question) is a bit much... maybe you should think about a different data model (Map, List, ...) Commented Jan 20, 2022 at 9:17
  • 1
    @SSinha please have a look - mkyong.com/java/java-properties-file-examples Commented Jan 20, 2022 at 9:19
  • 1
    Just use instance variables instead of static variables and two objects of the same class. Commented Jan 21, 2022 at 8:01

2 Answers 2

1

If abc is gonna be always the same you can just keep the one in the parent class, you will always be able to access it whether your instance is of a parent or a child class.

If the value of abc is different per child class, create a method public String getAbc() in the parent class and override it in each child that needs a different value:

public class Fruits {
   public static string abc="something";

   public String getAbc() {
      return abc;
   } 
}

public class Apple extends Fruits {
   public static string abc="something different";

   @Override
   public String getAbc() {
      return abc;
   } 
}

And so, in your final call use it this way:

Utility.getfruit().getAbc();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I understood but a little problem I have over more than 1500 variables so do I have to generate getter for all ?
You can give it a try using Lombok. Although I don't know if they have something for gettters for static fields.
0

Add a method in the parent class Fruits and override that method in child class Apple .

when you call that method with instance that you got from getfruit will work.

sample code

public class UtilityDemo {​​​​
    static int config = 3;
    public static Fruits getfruit() {​​​​
        if (config == 3) {​​​​
            return new Fruits();
        }​​​​ else
            return new Apple();
    }​​​​

    public static void main(String args[]) {​​​​
        System.out.println(getfruit().getAbc());
    }​​​​
}​​​​


public class Fruits {​​​​
    public static String abc = "From Fruits";
    public String getAbc() {​​​​
        return abc;
    }​​​​
}​​​​


public class Apple extends Fruits {​​​​
    public static String abc = "From Apple";
    @Override
    public String getAbc() {​​​​
        return abc;
    }​​​​
}​​​​

5 Comments

but why is there a need for an instance? There aren't instance variables.
Thanks I understood but a little problem I have over more than 1500 variables so do I have to generate getter for all ?
they are not instance variables but Question asked is they want instance of class right? @Stultuske
That's how it is @SSinha we need to generate Getters and Setters for all the variables.
@SRMKumar yes, the question is, the example provided isn't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.