Java Programming Tutorial - Many Methods and Instances


import java.util.Scanner; 

class apples{
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
Coffee CoffeeObject = new Coffee(); 
System.out.println("Enter name of first gf here: ");
String temp = input.nextLine (); 
CoffeeObject.setName (temp);
CoffeeObject.saying(); 
}
}



public class Coffee{
private String girlName; (variable)(private is only the method in this class can use it)
    public void setName (String name) { (Set the name for the girl name)
    girlName=name;  (two variables name and girl name)
}
    public String getName() { 
    return girlName;
    
    }
    public void saying () {
    System.out.printf("Your first gf was %s", getName());
    }
}

instance variables

Comments