Key Concepts

Review core concepts you need to learn to master this subject

Java objects’ state and behavior

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

In Java, instances of a class are known as objects. Every object has state and behavior in the form of instance fields and methods respectively.

Constructor Method in Java.

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

Java classes contain a constructor method which is used to create instances of the class.

The constructor is named after the class. If no constructor is defined, a default empty constructor is used.

Java instance

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

Java instances are objects that are based on classes. For example, Bob may be an instance of the class Person.

Every instance has access to its own set of variables which are known as instance fields, which are variables declared within the scope of the instance. Values for instance fields are assigned within the constructor method.

Creating a new Class instance in Java

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

In Java, we use the new keyword followed by a call to the class constructor in order to create a new instance of a class.

The constructor can be used to provide initial values to instance fields.

Java dot notation

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

In Java programming language, we use . to access the variables and methods of an object or a Class.

This is known as dot notation and the structure looks like this-

instanceOrClassName.fieldOrMethodName

Java method signature

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

In Java, methods are defined with a method signature, which specifies the scope (private or public), return type, name of the method, and any parameters it receives.

The body of a Java method

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

In Java, we use curly brackets {} to enclose the body of a method.

The statements written inside the {} are executed when a method is called.

Java Variables Inside a Method

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

Java variables defined inside a method cannot be used outside the scope of that method.

Returning info from a Java method

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

A Java method can return any value that can be saved in a variable. The value returned must match with the return type specified in the method signature.

The value is returned using the return keyword.

Method parameters in Java

public class Person { // state of an object int age; String name; // behavior of an object public void set_value() { age = 20; name = "Robin"; } public void get_value() { System.out.println("Age is " + age); System.out.println("Name is " + name); } // main method public static void main(String [] args) { // creates a new Person object Person p = new Person(); // changes state through behavior p.set_value(); } }

In java, parameters are declared in a method definition. The parameters act as variables inside the method and hold the value that was passed in. They can be used inside a method for printing or calculation purposes.

In the example, a and b are two parameters which, when the method is called, hold the value 10 and 20 respectively.

Chevron Left Icon
Java: Introduction to Classes
Lesson 1 of 2
Chevron Right Icon
  1. 1
    All programs require one or more classes that act as a model for the world. For example, a program to track student test scores might have Student, Course, and Grade classes. Our real-world concer…
  2. 2
    The fundamental concept of object-oriented programming is the class. A class is the set of instructions that describe how an instance can behave and what information it contains. Java has pre-d…
  3. 3
    We create objects (instances of a class) using a constructor method. The constructor is defined within the class. Here’s the Car class with a constructor: public class Car { public Car() { /…
  4. 4
    Our last exercise ended with printing an instance of Store, which looked something like Store@6bc7c054. The first part, Store, refers to the class, and the second part @6bc7c054 refers to the insta…
  5. 5
    We’ll use a combination of constructor method and instance field to create instances with individual state. We need to alter the constructor method because now it needs to access data we’re assig…
  6. 6
    Now that our constructor has a parameter, we must pass values into the method call. These values become the state of the instance. Here we create an instance, ferrari, in the main() method with “r…
  7. 7
    Objects are not limited to a single instance field. We can declare as many fields as are necessary for the requirements of our program. Let’s change Car instances so they have multiple fields. We…
  8. 8
    Java is an object-oriented programming language where every program has at least one class. Programs are often built from many classes and objects, which are the instances of a class. Classes def…
  1. 1
    In the last lesson, we created an instance of the Store class in the main method. We learned that objects have state and behavior: We have seen how to give objects state through instance fiel…
  2. 2
    Remember our Car example from the last lesson? Let’s add a method to this Car called startEngine() that will print: Starting the car! Vroom! This method looks like: public void startEngine() {…
  3. 3
    Great! When we add the startEngine() method to the Car class, it becomes available to use on any Car object. We can do this by calling the method on the Car object we created, for example. Here …
  4. 4
    A method is a task that an object of a class performs. We mark the domain of this task using curly braces: {, and }. Everything inside the curly braces is part of the task. This domain is called t…
  5. 5
    We saw how a method’s scope prevents us from using variables declared in one method in another method. What if we had some information in one method that we needed to pass into another method? Sim…
  6. 6
    Earlier, we thought about a Savings Account as a type of object we could represent in Java. Two of the methods we need are depositing and withdrawing: public SavingsAccount{ double balance; p…
  7. 7
    Remember, variables can only exist in the scope that they were declared in. We can use a value outside of the method it was created in if we return it from the method. We return a value by u…
  8. 8
    When we print out Objects, we often see a String that is not very helpful in determining what the Object represents. In the last lesson, we saw that when we printed our Store objects, we would see …
  9. 9
    Great work! Methods are a powerful way to abstract tasks away and make them repeatable. They allow us to define behavior for classes, so that the Objects we create can do the things we expect them …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo