Java Class, Object & Methods

In this chapter we learn about the Class, Object, Access modifiers, Non access modifiers & Methods.


What is a Class?

A Class is defined as the template or blueprint for an object. It defines the states and behavior of that object. When a class is created, we are letting the JVM know that how to make an object of that type. Apart from the Instance variables, & methods, the class also contains class variables and local variables.

What is a State?

The instance variables present in the class are said to be State.

What is a Behavior or Methods or Functions?

The methods present inside the class are said to be behavior. These are the things which the object does.

What is an Object?

An object is an instance of a class. The creation of object is called Instantiation. We will see the instantiation steps with the example given later in this page.

What is a Constructor?

A Constructor is method every class should have compulsorily. The constructor name should be equal to the class name. If the constructor is not explicitly created, the compiler will create a default constructor for the class.

Whenever we create an object, the constructor is called first and the content inside it is executed first. A class can have many constructors. We will discuss this in our Overloading chapter.

How to write your first Java program?

Goto Eclipse ->File -> New -> Project (Choose Java Project and Click Next) -> provide a project name and click Finish.

Now you will be able to see your project in the package explorer on the left side. Expand it. Right click on the src -> New -> Package (Provide a package name and click finish).

Right click on your newly created project -> New -> Class (Provide a class name, check the public static void main(String[] args) option and then click finish).

By doing the above steps, you will be getting a Java class with a empty main function. Try out example below and hit ctrl + F11 for the output.

Example: MyFirstClass.Java

package Tutorials;

// This is your first java class.
public class MyFirstClass{

// Class variable.
private String firstVariable ="HI, I am your first class variable.";

// The constructor name should be equal to this class name.
MyFirstClass(){
System.out.println("Hi, I am your first Constructor.!!");
}

// This is our main method.
public static void main(String[] args){

  // The below code 'System.out.println()' can be used to print our outputs in the console. 
  System.out.println("This is my first Java Program!!");

  /**
   * This is how we need to create an object using the new keyword. 
   * The constructor will be called first when we create an object first.
   */
  MyFirstClass firstObject =new MyFirstClass();

  // Invoking your first variable using the created object.
  System.out.println("Printing your FirstVariable = "+ firstObject.firstVariable);

  // Invoking your first method using the created object.
  System.out.println("Calling you first method = "+ firstObject.myFirstMethod());

  // This is how you write a single line comment, to give a small description.

  /**
   * This is a sample of multi-line comments.
   * The comments are ignored by the compilers. 
   */
}
  /**
   * This is your first java method.
   * @return default text
   */
  public String myFirstMethod(){
  return"Hi, I am your first method.";
}
}

Console Output:
This is my first Java Program!!
Hi, I am your first Constructor.!!
Printing your FirstVariable = HI, I am your first class variable.
Calling you first method = Hi, I am your first method.

Posted on July 1, 2014 in Java for Beginners

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top