Skip to content

Instantly share code, notes, and snippets.

@rishisidhu
Last active December 21, 2019 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishisidhu/291f9786e3b7c0b6c9036461b4e3440c to your computer and use it in GitHub Desktop.
Save rishisidhu/291f9786e3b7c0b6c9036461b4e3440c to your computer and use it in GitHub Desktop.
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