• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Classes » What is a Class in Java? – Definition
Next →
← Prev

What is a Class in Java? – Definition

By Dinesh Thakur

Classes are the fundamental building blocks of any object-oriented language.

A class describes the data and behaviour associated with instances of that class. When a class is instantiated, an object is created: this object has properties and behaviour similar to other instances of the same class. The data associated with a class or object is stored in variables. The behaviour associated with a class or object is implemented by means of methods. Methods are similar to the functions or procedures of procedural languages such as C or Pascal.

A class can be defined as follows:

class First { // Body of the class }

The keyword class is used to define a class. The keyword is followed by the name of the class (in the above example First). The body of the class is contained within curly brackets. The body consists of statements related to constructors, methods and variables.

Every class defined in Java is a child of the Object class.

A class may have any of these variable types:
● Local: defined within a constructor, block or method. The variable is declared and then initialised inside a method and destroyed once the method has ended.
● Instance: defined in a class but are outside a method. Initialised when instantiation of the class happens and can access from inside any constructor, block or method of the class.
● Class: declared inside the class, uses the static keyword and are outside any method. 

We’ll be covering the following topics in this tutorial:

  • Creating instance and class variables
  • Constructors
  • Creating Objects

Creating instance and class variables

A class usually contains variables and methods. Certain specific rules have to be followed for defining variables and methods within a class. This section deals with the definition of different types of variable, namely instance variables, constants and class variables.

Instance variables

Instance variables are declared and defined in almost the same way as local variables, the main difference being their location in the class definition. Variables are considered instance variables. if they are declared outside a method definition. It is customary to define instance variables just after the first line of the class definition.

Constants

A constant variable or constant is a variable whose value never changes.

Constants are used to define shared values for all the methods of an object so that object-wide values that will never change can be given meaningful names. In Java, only instance and class variables can be constants (not local variables). To declare a constant, the keyword final is used. It should be placed before declaration of the variable and should include an initial value for that variable, as shown in the following examples:

final float pi = 3.14:

final boolean debug = false;

final int maxsize = 40000;

Constants can be useful for naming various states of an object and testing them. For a test label that can be aligned left, right or centre, the values can be defined as constant integers.

final int LEFT = 0;

final int RIGHT = 1;

final int CENTER= 2;

Class variables

Class variables are global to a class and to all instances of that class. Class variables are used for communication between different objects within the same class. These variables are also used for tracking global states among a set of objects. The static keyword is used in the class declaration to declare a class variable. Some examples of this are given below:

static int sum;

static final int maxObjects = 10;

Constructors

Every class have a constructor; if you omit it, a default one built by the compiler. When you create a new object, at least one constructor must invoke.  As a rule, the name of a constructor must be the same as that of the class, and there can be as many constructors as a class requires.  It is what a constructor looks like: 

publicclass Puppy {
public
Puppy(){}
public Puppy(String name){
// This constructor has a single parameter, name.
}

}

Creating Objects

We already know that a class is a kind of blueprint to create objects from so the object is created from the class. For a new object to be cre-  ated, we need the new keyword.
These are the three steps needed to create an object from a class:
● Declaration:  a variable must declare with a name and the object type
● Instantiation: the new keyword use for creating the object.
● Initialisation: the constructor called, and this initialises the object.
The next example shows how objects create:

public class Puppy {
public
Puppy(String name) {
// This constructor contains a single parameter called name.
System.out.println(“Passed Name is :” + name );
}
    public static void main(String []args) {
// The next statement create an object called myPuppy
Puppy myPuppy = new Puppy( “fluffy” );
}
}

You’ll also like:

  1. c++ – Defining member functions inside or outside the class definition
  2. Java Classes and Objects (With Example) – Definition
  3. Inner class in java with Example
  4. Nested Class in Java Example
  5. Class methods in Java
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW