Java – Class and Object

In the previous tutorial, we had discussed the OOPs concept in Java. In this chapter, we will learn how to define a class and creating an object in Java. We will also have an example of accessing data members and member functions of a class.

Define a Class In Java

Let’s start with how to define a class in Java. To create a class the ‘class’ keyword is used with the name of a class. All the members of the class must be within curly { } brackets.

The syntax for defining class.

class Product
{
	//variables
	//methods
}

The syntax for creating an Object.

class-name object-name=new class-name();
Product mobile=new Product();
  • object – In the above example, the name of the object is ‘mobile’;
  • new – The new keyword is used to create an object by allocating memory on the computer.

Example: Here we will create a class Product with some attribute(variable).

//defining class 
class Product
{
	//attribute declaration
	String pname;
	int price;

public static void main(String args[])
{
	//object declaring
	Product prod=new Product();

	//access member with object reference
	prod.pname="Mobile";
	prod.price=10000;
	System.out.println("Price of "+prod.pname+" is "+prod.price);
}
}

In the above example, the class Product has two attributes (pname and price) outside the main method. Inside the main method, we need to create an object to access the attributes of the class.

Example: An example of behavior(methods) inside the class.

//defining class 
class Example
{
	//defining method
	public void msg()
	{
		System.out.println("Hello Coder");
	}	

public static void main(String args[])
{
	//object declaring
	Example ex=new Example();

	//access member with object reference
	ex.msg();
}
}

Multiple Objects of a class

In Java, we can declare multiple objects of a class.

//defining class 
class Color
{
	//attribute declaration
	String name;

public static void main(String args[])
{
	//multiple object declaration
	Color c1=new Color();
	Color c2=new Color();
	Color c3=new Color();

	//access member with object reference
	c1.name="Red";
	c2.name="Green";
	c3.name="Blue";
	System.out.println(c1.name);
	System.out.println(c2.name);
	System.out.println(c3.name);
}
}

More than One Class(Multiple Class)

A program can have more than one class in Java. But the main method should be within only one class. We can access a member of the class with the class object reference.

//defining first class 
class First
{
	public void firstMsg()
	{
		System.out.println("First Class");
	}
}

//defining second class
class Second
{
	public void secondMsg()
	{
		System.out.println("Second Class");
	}

public static void main(String args[])
{
	//object of both class
	First f=new First();
	Second s=new Second();

	//access member with object reference
	f.firstMsg();
	s.secondMsg();
}
}

Nested class in Java (class within a class)

In java, we can define a new class within another existing class. The existing class is called the outer class and the new class is called the inner class.

//outer class
class Outer
{
	//defining Inner class
	class Inner
	{
		public void msg()
		{
			System.out.println("Hello");
		}
	}

public void show()
{
	//creating object and calling method of Inner class
	Inner in=new Inner();
	in.msg();
}
public static void main(String arg[])
{
	//creating object of Outer class
	Outer ot=new Outer();
	ot.show();
}
}