This tutorial will guide you on what is a class and how does Java deal with objects. You will know how to create a class and instantiate its object. These are the basic building blocks of object-oriented programming (OOP) in Java.

Basics Of Classes And Objects In Java

You can go through the following sections to learn about Java Class.

Contents [hide]

1 Object-Oriented Programming

2 Class in Java

3 Object in Java

4 Java Class – Example

5 Variable Types:

5.1 Class variables:

5.2 Local variables:

5.3 Instance variables:

6 Related Posts


Object-Oriented Programming

Many a time you must have come across the phrase Java is an Object-Oriented Programming Language. The term Object-Oriented denotes a concept in software development.

It is a way of organizing software in the form of objects that contain both data and the behavior of these objects. Therefore, Object-Oriented Programming Languages, generally known as OOP, provide the programming model for simplifying for software development, design and maintenance under some well-established ground rules.

The projects made in OOPS are more structured towards objects. As a result, it increases performance, maintainability, and development of the program. The main idea behind OOP is to incorporate data and behavior under the same location(objects).

The fundamental concepts supported by OOPS are:

  • Classes
  • Objects
  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction

Class In Java

A class in Java or any other Object-oriented Language is a blueprint for objects to follow a specific schema defined in the class.

Classes define the behavior for objects of its type. It represents a collection of properties (data and functions) for all its objects.

It supports a template for creating objects which bind code and data. Further, classes act as a means to define methods and data. It helps in maintaining access specifications for member variables using access specifiers.

Object In Java

An Object is the most fundamental entity in Java or any other Object-Oriented Language. Objects represent real-life entities because each of them could have specific behavior, identity, and data (attributes).

In Java, the object is an offspring of its class. The class has properties to reflect the object state and methods to represent the behavior.

The methods also show an object’s response to other objects. Identity is a unique name for the object assigned by the user, much like variables.

Let’s have a profound look into what are objects. If we think about this present reality, we can discover numerous articles around us, vehicles, people, and so on. Every one of these has a unique state and behavior. You can’t expect a human being to bark like a dog or a dog to speak like a human.

For example – A car, its state are – name, model no, shade, manufacturer and its behavior can be – moving, blinking the headlights, honking, etc.

If you try to compare a Java object with any real-time entity, they could probably have fundamentally same attributes.

Must Read – Java Multithreading

Java Class – Example

public class Car {

// Class Attributes - State of an object
String color;
int model_no;
String name;
String manf;

// Class Methods - Behaviour of an object
void honk() {
}

void move() {
}

void blink() {
}
}

A class can have any number of functions to access the properties of the class’s object or manipulate the properties. In the above example, move(), blink() and honk() are a few methods.

Variable Types:

A class can contain any of the accompanying variable sorts:

Class Variables:

A class variable is one which has the static keyword as a prefix in its declaration. Its definition occurs only inside a class and outside any function.

Local Variables:

These are variables which have declarations inside methods, constructors or blocks. They are local to the part of the code they belong.

Local variables come into existence when the control enters into the code block that keeps their declaration. And they vanish with the block leaving out of execution.

Instance Variables:

These variables are inside a class however outside any method. They come into existence when the class instantiates. These are accessible from any constructor or block of that specific class.

Related Posts

Constructor in Java

Inheritance in Java

Originally published on https://www.techbeamers.com


#java

Class and Object in Java – OOPs
2 Likes9.60 GEEK