What is Java Class and Object?


In this Java Class and Object tutorial, we will learn about the fundamentals of Java Programming. Java Class and Object is the base of Java. And we have tried to explain it in a simpler way. For your better understanding we have also provided example Java programs for concepts Java Classes and Object and Java Constructors.


Classes and Objects are the fundamental building blocks of OOPS (Object Oriented Programming). A java object is a physical and logical entity where as java class is logical entity.

Table of Content

1. What is a Java Class?
  • Create your first Java program "Hello World"
  • Explanation of Java keywords used in "Hello World" program

2. What is a Java Object?
  • How to create Java Object?
  • What is the use of Java Objects?

3. What is Java Constructor?
  • Non-Parameterized Constructor (Default)
  • Parameterized Constructor


1. What is a Java Class?

Before creating an object in Java, you need to define a class. A class is a blueprint from which object is created. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object. Since, many houses can be made from the same description, we can create many objects from a class. A class can have following entities inside it:

  • Fields (Variables)
  • Methods
  • Constructor
  • Nested Class
  • Interface
  • Blocks
Syntax to define a Java Class:
public class Dog {

   String breed;
   int age;
   String color;

   public void barking() {
   }

   public void hungry() {
   }

}
Explanation of the example code:
  • Variables like breed, age and color are attributes or exhibits state.
  • Where as barking() and hungry() are methods which exhibits behavior.
Let's understand these concepts from an example:

Class: 

Animal

Objects:

Dog

Cat

Cow

Now, you can relate the definition that Class is a template and object is an instance of the class.


Java Class and Object Video Tutorial



1.1. Write your First Java Program - Hello World

It's always exciting to write your first program and execute it without error. In this post you will learn to write your first java program.
Creating Hello World Program
  • Declare a class 'MyFirstProgram'
  • Declare the main method public static void main()
  • And print a string "Hello World" using System.out.println()
Hello World Program

// Class
class MyFirstProgram {
 
 // Main method
 public static void main(String[] args) {
  
  // Print Hello World
  System.out.println("Hello World");
 } 

}

1.2. Explanation of Java keywords used in 'Hello World' Program:

  • class keyword is used to declare a class.
  • Public is an access modifier which defines main method's accessibility. Public means main method is global or accessible to all other classes in the project.
  • static is a keyword in java, when static is used with any method then it is called static method. Static method can be called without creating the object of the class.
  • main is the method name and also the execution point of any program. Main method is required to execute the program.
  • String[] args is an array of string is basically for command line arguments.
  • System.out.println() is used to print any statement. 
    • System is a class
    • out is the object of PrintStream class
    • println() is method of PrintStream class

Run your First Java Program:
  • If you are using Eclipse then you can run this program by just clicking the green play button displayed on the top bar. 
  • Or right click anywhere in the class and click 'Run as Java Program'.

Output:
  • Hello World


2. What is a Java Object?

We often read in our text books that "Object is an instance of a class". An object has a state and behavior. For example., a dog has states - color, name and behavior as well like barking, eating etc. 

The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes. In Java, an object is created using the keyword "new".


2.1. How to create a Java Object?

Syntax:

MyClass myObject = new MyClass();


In above example, 

  • MyClass is class name.
  • 'myObject ' is the object name (object name can be anything) 
  • 'new' is keyword of java which instantiates the object (in simple language it creates the object).
  • 'MyClass()'is the constructor of 'MyClass' which is called whenever an object is created.

2.2. What are the uses of Java Objects?

Java Object is used for many purposes, which are listed as follows:
  • We can call methods of a class by using object of that class with dot (.) operator.
  • Similarly, we can call variables of a class by using object of that class with dot (.) operator.

3.  What is Java Constructor?

In java a constructor is called a special method. It is used to create objects in java. A constructor is called whenever we initialize an object. 

Some special features of Java Constructor:

  • It has same name as that of a class.
  • It does not have any return type.
  • It's syntax is similar to method.
  • A java constructor cannot be static, final or abstract.
  • A class always have a default constructor whether you have declared it or not. But if you define a custom constructor then default constructor is no longer in use.

Java Constructor Video Tutorial



3.1. Types of Constructors in Java:

  • Non-Parameterized Constructor (Default Constructor)
  • Parameterized Constructor

3.1.1. Non-Parameterized Constructor

As the name suggests, this constructor doesn't have any parameter in it's signature.

Syntax for Default Constructor:

public class MyClass{  
	
	// Creating a default constructor  
	MyClass(){
		System.out.println("Object created");
	}  

	// Main method  
	public static void main(String args[]){  
		//Calling a default constructor  or Creating an object of MyClass
		MyClass obj = new MyClass();  
	}  
}  

Explanation of example code:

  • We have created a default constructor "My Class", which will print "Object created" whenever we create an object.
  • In main method, we are calling the constructor to initialize our object named "obj".
Output:
Object Created

3.1.2. Parameterized Constructor

It's a constructor which accepts parameters. It can have one or more parameters. It is used to provide different values to different objects.

Syntax for Parameterized Constructor:

public class MyClass {

  // Declare variable
  int num;

  // Parameterized Constructor
  public MyClass(int y) {
    num = y;
  }

  public static void main(String[] args) {
    // Call the parameterized constructor and pass parameter 5
    MyClass myObj = new MyClass(5);
    // Print num
    System.out.println(myObj.num);
  } 
}

Explanation of the example code:
  • We declared a variable num.
  • Declared a parameterized constructor with parameter int y.
  • Inside constructor we are initializing num's value to y.
  • In main method, we are calling parameterized constructor and passing value 5 to y. It will initialize num to 5 inside the parameterized constructor.
  • Now the value of the num is set to 5 and it'll print 5.
Output:
5



Next >>  Java Data Types and Variables

Follow Techlistic

YouTube Channel | Facebook Page | Telegram Channel | Quora Space
Feel free to ask queries or share your thoughts in comments or email us.

Comments

  1. oh good! you bring good & unique content for us. I'm glad to read. Please keep sharing. - Mahindra Jivo 365

    ReplyDelete
  2. You are giving such interesting information. It is great and beneficial info for us, I really enjoyed reading it. Thankful to you for sharing an article like this.dft training in bangalore

    ReplyDelete
  3. Thanks for sharing beneficial tutorial. Web development, design and All SEO service provider Houston Digital Marketing Agency

    ReplyDelete
  4. The blog you have shared is awesome about C Programming Assignment Help This is very useful for us. Thanks for sharing such a good blog.

    ReplyDelete
  5. Good to read this article when starting a blog. Happy to read that is only a limitation in our minds Thanks!

    ReplyDelete
  6. This article was such an amazing masterpiece. I really want to know how did you get this information? I’m really thankful to you to provide this knowledge. Tablet hire is providing the best services for tablets, iPad, laptops, and many other technical devices at rent. we serve our services at a very responsible price.
    tab rentals

    ReplyDelete

Post a Comment

Popular posts from this blog

10+ Best Demo Websites for Selenium Automation Testing Practice

Automate Amazon like E-Commerce Website with Selenium WebDriver

How to Automate Google Search with Selenium WebDriver

Handle Static and Dynamic Web Table in Selenium WebDriver

25+ Most Important Selenium WebDriver Commands Tutorial

Automate GoDaddy.com Features with Selenium WebDriver

Top 10 Highly Paid Indian Origin CEO's in USA

50+ Most Important Selenium WebDriver Interview Questions and Answers