Classes And Objects In Java

Course Home | What are Classes and Objects? | Code Example(s)

With Java being an Object-orientated programming (OOP/OO) language, classes and objects are what it’s all about. Everything when coding with Java is an object.

Within IntelliJ, it’s very easy to create a new class, in the project window we right-click where we want the class to be and click New > Java Class. IntelliJ will then show us a prompt to give our class a name. As mentioned in the Basic Programming course an object is an instance of a class. So we need to give the class a name that will make sense when using it in our code.

Here is the bare minimum we need to create a new class

public class MyClass
  //Make our class do things here

We can use this class in our other classes by creating a new instance of it. We do this using the new keyword.

public class MySecondClass
  MyClass instanceOfMyClass = new MyClass();

We can then use that instance, which we call an object, in our code. In a complete solution, it will have methods and values I wish to use.

As you start writing more code in Java you’ll most certainly create many classes, and you’ll also use lots of objects from libraries that you add to your project.

Next Lesson ➔

Richard Bradshaw's photo

Richard Bradshaw


Software Tester, speaker and trainer at Friendly Testing. BossBoss at Ministry of Testing. Whiteboard Testing creator. Striving to improve the testing craft.