Thursday, June 18, 2026

2. 0 java Class

}  Class can be defined in multiple ways

Ê  A class is a blueprint for an object.

Ê  A class is a user-defined data type.

Ê  A class is a collection of objects of the similar kind.

}  A class is a user-defined data type that contains:

Ê  Attributes (Variables) → Data(data members

Ê  Methods (Functions) → Behavior(member functions



}  Class name can be given as per the Identifier Naming Conventions.

Defining a Class in Java

All it takes to define a class in Java is this:

public class MyClass {

 

}

This defines a public Java class called MyClass. The class has no fields, constructors or methods.

.java Files

The above class definition should be put in its own file named MyClass.java. Java files should be named the same as the name of the class they contain, with the .java as file name extension. Make sure you keep the same uppercase and lowercase characters from the class name in the file name too.

Only put a single class definition in each Java file, unless your class contains inner classes of some kind.

Class With Fields

As mentioned earlier, a Java class can contain data in the shape of variables. Variables that belong to the class are typically called "fields".

The next example shows a Java class which is to model a car. Therefore the class has named Car and has three fields. Here is the Java class in code:

public class Car {

 

    public String brand = null;

    public String model = null;

    public String color = null;

 

}

This code defines a Java class named Car. The Car class has three fields. The Car class has no methods. Only field declarations. Fields are described in more detail in the text on java fields

Class With Constructor

A Java class can have a constructor. A constructor is a special method that is called when an object of the given class is created (explained later). The purpose of a constructor is to initialize the fields in the class. The fields are also called the "internal state". Here is an example of a Java class with two constructors:

public class Car {

     public String brand = null;

    public String model = null;

    public String color = null;

     public Car() {

    }

public Car(String theBrand, String theModel, String theColor) {

        this.brand = theBrand;

        this.model = theModel;

        this.color = theColor;

    }

 

}

The constructors are the two methods that have the same name as the class, and which have no return type specified. The first constructor takes no parameters, and the second takes 3 parameters. The constructor that takes 3 parameters stores the values of these parameters in the fields of the created object. Constructors are covered in more detail in constructor tutorial

Class With Methods

A Java class can also contain operations. These operations are typically called methods. A Java method contains Java instructions that typically perform some operations on a field in the class, or on one of the parameters (also variables) values passed to the method when the method was called.

Here is the Java class, Car example from the previous section with a method added:

public class Car {

 

    public String brand = null;

    public String model = null;

    public String color = null;

 

    public void setColor(String newColor) {

        this.color = newColor;

    }

}

In the class definition above I have added a setColor() method. When called, this method sets the internal color variable (field) to a new value. Methods are described in more detail in the text on methods

Class With Nested Class

As mentioned earlier, you can define a nested class inside another Java class.

Here is an example of defining a nested class inside a Java class:

public class MyClass {

 

    public static class MyNestedClass{

 

    }

}

In the example above, the outer class is called MyClass and the nested class is called MyNestedClass .

Neither of the classes in this example has any fields or methods, but both the outer and nested class could have as many fields and methods as you see fit.

 


No comments:

Post a Comment

2. 0 java Class

}   Class can be defined in multiple ways Ê   A class is a blueprint for an object. Ê   A class is a user-defined data type. Ê   A cla...