Javatpoint Logo
Javatpoint Logo

Java Class Notation

Java is a popular object-oriented programming language that is used to create powerful and efficient software applications. In Java, a class is the fundamental unit of code, and it defines the blueprint for objects. Every object in Java belongs to a class, and a class consists of methods, variables, and other components that define the behaviour of the objects created from it. In this article, we will explore the notation used to define a Java class, including the syntax and semantics of the code. We will also provide some example programs with outputs to demonstrate the different features of a Java class.

In Java, a class is defined using the "class" keyword followed by the name of the class. The name of the class should start with an uppercase letter and follow the camel-case notation. For example, if we want to create a class for a car, we can define it as follows:

In the example above, we have defined a class named "Car" using the "public" access modifier. The access modifier specifies the visibility of the class, which can be public, private, or protected. A public class can be accessed from any other class in the same package or a different package, whereas a private class can only be accessed within the same class. A protected class can be accessed within the same package or a subclass in a different package.

Class Members

A class can have various members, such as methods, variables, constructors, and inner classes. Let's take a look at each of these members in detail.

Methods

A method is a set of instructions that performs a specific task. It is defined within a class and can be called from other classes to perform the task it was designed for. In Java, a method is defined using the "public" access modifier followed by the return type, method name, and the parameters (if any). For example, let's define a method named "start" in the Car class that prints a message to the console:

In the example above, we have defined a method named "start" that returns nothing (void) and takes no parameters. The method simply prints a message to the console using the System.out.println() method.

Variables

A variable is a named location in memory that stores a value. It can be of different types, such as integer, float, double, boolean, and string. In Java, variables can be defined within a class or a method. They can also be static or non-static. A static variable belongs to the class and is shared by all objects of the class, whereas a non-static variable belongs to the object and is unique to each object.

Let's define a static variable named "numberOfCars" in the Car class that keeps track of the number of cars created:

In the example above, we have defined a static variable named "numberOfCars" that is initialized to zero. We have also defined a constructor that increments the value of "numberOfCars" every time a new object of the Car class is created.

Constructors

A constructor is a special method that is called when an object of a class is created. It is used to initialize the variables and perform any other setup tasks required for the object to function properly. In Java, a constructor has the same name as the class and no return type. For example, let's define a constructor for the Car class that sets the make and model of the car:

In the example above, we have defined a constructor for the Car class that takes two parameters, "make" and "model", and sets the corresponding instance variables using the "this" keyword.

Inner Classes

An inner class is a class that is defined within another class. It can access the members of the outer class and vice versa. In Java, an inner class can be static or non-static. A static inner class belongs to the outer class and can be accessed without creating an object of the outer class. A non-static inner class belongs to an object of the outer class and can only be accessed through that object. Let's define a non-static inner class named "Engine" in the Car class that has a method named "start" to start the engine:

Output:

The engine has started

In the example above, we have defined a non-static inner class named "Engine" in the Car class that has a method named "start" to start the engine.

Now that we have covered the syntax and semantics of Java classes, let's take a look at some example programs to demonstrate the different features of a Java class.

Example 1: Car Class

Output:

The Toyota Camry has started
The Toyota Camry has stopped

In the example above, we have defined a Car class with instance variables for the make, model, and year of the car. We have also defined methods to start and stop the car, which print messages to the console indicating that the car has started or stopped. In the main method, we create an object of the Car class and call the start and stop methods on it.

Example 2: Employee Class

Output:

Name: John Doe
Salary: $50000.0
Name: John Doe
Salary: $55000.0

In the example above, we have defined an Employee class with instance variables for the name and salary of the employee. We have also defined methods to raise the employee's salary by a certain percentage and to display the employee's name and salary. In the main method, we create an object of the Employee class and call the display method to print the employee's initial name and salary. We then call the raiseSalary method to increase the employee's salary by 10%, and call the display method again to print the updated name and salary.

Example 3: BankAccount Class

Output:

Account Number: 123456789
Account Holder: John Doe
Balance: $1000.0
Account Number: 123456789
Account Holder: John Doe
Balance: $1500.0
Insufficient funds
Account Number: 123456789
Account Holder: John Doe
Balance: $1500.0

In the example above, we have defined a BankAccount class with instance variables for the account number, account holder, and balance. We have also defined methods to deposit and withdraw money from the account, and to display the account information. In the main method, we create an object of the BankAccount class and call the display method to print the account information. We then call the deposit method to add $500 to the account, and call the display method again to print the updated balance. Finally, we call the withdraw method with an amount of $2000, which is more than the current balance, and the method prints a message indicating that there are insufficient funds.

In summary, a class is a fundamental building block of object-oriented programming. It defines the properties and behaviours of objects that belong to that class. The class notation in Java is used to define classes and their properties and behaviours. We covered the syntax and semantics of Java classes, including class access modifiers, constructors, instance variables, methods, and inner classes. We also provided examples of Java programs that demonstrate different features of a Java class, including a Car class, an Employee class, and a BankAccount class. Understanding Java classes is essential for developing Java applications, and it is a crucial aspect of Java programming that developers must master.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA