How to define a class in java

 Class

A class is the blueprint of the objects that are derived from it.A class consists of data members and methods.For example:

  • Data members:

    • Company Name

    • Location

    • Package in Lacs

  • Method

    • Work()

Writing a class

    A class can be written as follows


public class DreamCompany

{

public String companyName;

public String location;

public String packageInLacs;


public void work()

{ // work in the company

}


}

Function definition is also given as and when function is declared.

Class declaration only specifies the template/blueprint and does not allocate any memory to the data members.

Memory is allocated only when objects are created from this class.Class declaration will be done only once.

We can create multiple objects from the same class in a java program


Main method in java

Every java application must contain a main method whose signature looks like this:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}





Comments

Popular posts from this blog

Enhanced for - loop or for-each loop

Arrays

Constructors