Java Class

In Java everything is encapsulated under classes. Class is the core of Java language. It can be defined as a template that describe the behaviors and states of a particular entity.

A class defines new data type. Once defined this new type can be used to create object of that type.

Object is an instance of class. You may also call it as physical existence of a logical template class.

In Java, to declare a class class keyword is used. A class contain both data and methods that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

Thus, the instance variables and methods are known as class members.

Rules for Java Class

A Java class can contains fields, methods, constructors, and blocks. Lets see a general structure of a class.

Java class Syntax

class Student.

{

              String name;

 int rollno;

              int age;

void info(){

                             // some code

}

}

 

The fields declared inside the class are known as instance variables. It gets memory when an object is created at runtime.

Methods in the class are similar to the functions that are used to perform operations and represent behavior of an object.

Java Object

Object is an instance of a class while class is a blueprint of an object. An object represents the class and consists of properties and behavior.

Properties refer to the fields declared with in class and behavior represents to the methods available in the class.

In real world, we can understand object as a cell phone that has its properties like: name, cost, color etc and behavior like calling, chatting etc.

So we can say that object is a real world entity. Some real world objects are: ball, fan, car etc.

There is a syntax to create an object in the Java.

Java Object Syntax

className variable_name = new className();

Here, className is the name of class that can be anything like: Student that we declared in the above example.

variable_name is name of reference variable that is used to hold the reference of created object.

The new is a keyword which is used to allocate memory for the object.

 

Powered by Froala Editor