DEV Community

vishnu vishnu
vishnu vishnu

Posted on

Class and Object in java

What is class?

  • A class in Java is a Blueprint or template from which an object is created. The object is defined by the Structure and behaviour(methods) that objects of that class will possess.

  • It is a logical entity.

  • No memory is allocated when a class is defined.

  • All classes are non-primitive datatypes.

How to create a class?

**
BASIC SYNTAX for creating a class:**

public class Car
{
    // Data members (fields)- hold  the actual value

String brand;
int speed;
    //Behaviour (method)
void drive()

{
System.out.println("driving a car");
}


}
//But it’s not a real car yet. It’s just the plan.

Enter fullscreen mode Exit fullscreen mode

Class Name Rules:

  1. Must start with an Uppercase first letter or (_) underscore or dollar sign ($).
  2. can not start with a number
  3. cannot contain spaces or special characters
  4. Use Pascal-case for naming

What is an Object?

  • An object is an instance of a class, and it has its own memory copy. It is a physical entity. An object is created with the new keyword.

  • When a class is created, no memory is used. Memory is allocated only when an object is created from that class.

Creating an Object :
classname, reference var,assignment operator,new keyword,classname();

Student id = new Student();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)