Skip to content

Classes and Instances

Sharina Stubbs edited this page Sep 21, 2019 · 10 revisions

Class

  • A blueprint or a template
  • Everything is built by a class somewhere that has certain properties
  • Class is the bakery; it's a building separate from making pastries

Making a class

  • Plan what has to be in the class ahead of writing the code

The Classes Job:

  • To set up the things that make an object unique
  • A place to set up details about all related objects, including what is true all the objects, and what is unique about the objects. A class might say, dough goes into the danish, no matter what flavor of danish. It also provides the details that it's a blueberry danish versus cheese danish
  • Sets up behavior for instances.

Class variables and methods:

Can be accessed before a single instance of the class has been made

Protected versus Public

  • If package deployed, someone can't use the protected methods.
  • If a method is public, you can use that method. Great use for protected involves working with username stuff

Instance

  • Objects in Java are always instances of classes, by definition
  • Object are the danish to the bakery.
  • Instance variables and methods can only be accessed from an instantiated instance

Example

If you have 3 different danishes, identify their differences

  • creamcheese flavor, circular shape, 20 minutes cooking time
  • blueberry flavor, triangle shape, 10 minutes cooking time
  • cinnamon danish, swirl shape, 600 minutes cooking time

In a file called Danish.java:

public class Danish {
    // pick out the unique characteristics about the danish
    String flavor;
    String shape;

    // choose an datatype that is big enough, but not too big to conserve memory. Check the range of a primitive type. A byte is too small, but a short is plenty. 
    Short cookTime;

    // Static means a property belongs to the class "Class"
    static String yumminess = "delicious";
    static String[] bakers = new String[]{"James", "Paula", "Michelle"};
    static String getDanishBaker(){
        return "James";
    }

    // must write a method to construct danishes. Name of the function will be the same name as the class_
    // basic template: public static Type NameOfMethod(params)
    public Danish Danish () { <--- this is the long way of writing it, and it's never written this way.
    } 

    // The real way to write it, so everyone knows it's a constructor: it has public  and nameOfClass/nameOfInstance:

    public Danish (String flavor, String shape, Short cookTime) {
    this.flavor = flavor;
    this.shape = shape;
    this.cookTime = cookTime;
    }

    public String toString(){  <----- declare this method because cheesy is in public static in our main method. If you use String toString(), it will print out to the terminal too. 
        // this is a round, cheesy Danish that has been cooked for 600 time units.
        
       return String.format("This is a %s, %s Danish that has been cooked for %d", this.shape, this.flavor, this.cookTime);
    }
   
}

In another file, called Bakery.java, which is main.

We need main to call everything regarding Danish, since public class Danish can't call itself.

public class Bakery {
    public static void main( String[] args) {

        Danish cheesy = new Danish("cheese", "circular", (short) 20); 
                                                             ^--------- note that you have to input this as a Short. When just written in as 20, Java thinks it will be an int, but it was declared above as a Short... so cast it.

       System.out.println(cheesy); // <------ will give us Danish@2a138343ad, which is where this danish lives in memory. Must write a method to get us this danish. 
       
       System.out.println(cheesy.toString() + cheesy.toString() + cheesy.toString())
       System.out.println(Danish.yumminess);
       System.out.println(Danish.getDanishBaker());
    
    }
}
Clone this wiki locally