Java Class and Object

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. In this tutorial, we will learn about Java Objects and Classes.

What is a Class in Java?

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.


What is Object in Java?

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".


Syntax for creating an object:

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.

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");
 } 

}

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


Java Control Statements  << Previous     ||     Next >>  Java Constructor

Follow Us

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

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation

Automate Google Search with Selenium Webdriver

How to Handle Dynamic Web Table in Selenium WebDriver