Key Concepts

Review core concepts you need to learn to master this subject

Java Objects

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

Java objects are instances of a Java class that contain state, in the form of instance fields, and behavior, in the form of methods. Java objects bring class into effect.

Class in Java with main() Method.

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

Java programs must contain one class with a main() method. main() method is invoked first. All other methods can be invoked via main() method.

Constructor Method in Java.

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

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.

Instance Fields in Java.

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

Java instances contain state in the form of instance fields: variables declared within the scope of the instance.

Values for instance fields are assigned within the constructor method.

Java Instances

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

A Java instance is created by the new keyword followed by invoking the constructor. Omitting the new keyword will cause an error.

For example, in this case, p is an instance of Person.

Person p = new Person();

Dot Notation in Java

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

In Java, dot notation . is used to access the value of fields and methods of an instance or Class.

The stucture used is instanceOrClassName.fieldOrMethodName.

Signature of a Java Method

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

Java methods are defined with a method signature, which includes:

  • whether the method is public or private
  • what data type the method returns
  • what the method is called
  • what parameters (if any) the method takes

Body of a Java Method

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

The body of a Java method is enclosed by brackets after the method signature. Everything inside the body will be executed when the method is called.

Java Variables Inside a Method

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

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

Return Value of a Java Method

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

Java methods can return a value. Any value that can be saved to a variable can be returned from a method using the return keyword. The type of variable returned must match the type specified in the method signature.

Parameters in Java

public class Print { public void toPrint() { System.out.println("Hello World!"); } public static void main(String [] args) { Print p = new Print(); p.toPrint(); } } //In this example, `Print` is a class and `toPrint()` is a method, which is called by an object `p`.

In Java, parameters are variables that are declared in a method definition. They hold value and can be used in a method’s operations.

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 […] , […] , and […] classes. Our real-world con…

  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-defi…

  3. 3

    We create instances with a constructor method defined within the class. Here’s the […] class with a constructor: […] The constructor, […] , shares a name with the class. We create …

  4. 4

    Our last exercise ended with printing an instance of […] , which looked something like […] . The first part, […] , refers to the class, and the second part […] refers to the instance’s…

  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, […] , in the […] method with …

  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 […] instances so they have multiple fields….

  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 […] 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 […] example from the last lesson? Let’s add a method to this […] called […] that will print: […] This method looks like: […] The first line, […] , is the _me…

  3. 3

    Great! When we add the […] method to the […] class, it becomes available to use on any […] object. We can do this by calling the method on the […] object we created, for example. …

  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…

  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: […] These methods would change the valu…

  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 […] objects, we would se…

  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