Java Basics: Objects

Hello again readers! Today we’re going to be taking a step back from all of these new features and go back to core Java design: objects!

Objects are the cornerstone of all Java development as Java is an (Object-Oriented Programming) OOP language. Each one is made with a state and behavior. Think of a blueprint. Classes act as blueprints for objects. Constructors use these blueprints/classes to instantiate objects as such:

public class myClass {
   public myClass(int num) {
      System.out.println("This is object number " + num);
   }

   public static void main(String[] args) {
      myClass obj = new myClass(1);
   }
}

All of this happens within class myClass. First, we create a constructor with one parameter: number. Second, we create our main method and create our object obj from our constructor. Our constructor contains our output which prints object number when run.

Its state is its number integer and its behavior is to print its object number. I demonstrate more use of objects in this video!

Follow my original code on my GitHub profile!

Leave a comment

Design a site like this with WordPress.com
Get started