Skip to content

Instantly share code, notes, and snippets.

Last active December 21, 2019 17:47
What would you like to do?
Code to create a java class and its object instances
public class Factory{
private String SolidFood1; // Solid Food 1st Instance Variable
private String SolidFood2; // Solid Food 2nd Instance Variable
private String LiquidFood1; // Liquid Food 3rd Instance Variable
public Factory(){} // 0-arg constructor (Not used)
public Factory(String sf1, String sf2, String lf1){ // 3-arg constructor
SolidFood1 = sf1;
SolidFood2 = sf2;
LiquidFood1 = lf1;
public String getSF1(){return SolidFood1;} // Method to get the name of 1st Instance Variable
public String getSF2(){return SolidFood2;} // Method to get the name of 2nd Instance Variable
public String getLF1(){return LiquidFood1;} // Method to get the name of 3rd Instance Variable
public static void main(String[] args){ // Main Method - Only this method gets executed
Factory lunchBox = new Factory("s1","s2", "l1"); // Object Created Yay!!!
System.out.println("Solid Food 1 is = " + lunchBox.getSF1()); // Call the methods to get the names and print them
System.out.println("Liquid Food 1 is = "+ lunchBox.getLF1());
System.out.println("Solid Food 2 is = "+ lunchBox.getSF2());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment