Creating a Java Class with Variable (Program Statement+Concepts+Sample Programs+Output)

PROGRAM STATEMENT

Create a Java class called Student with the following details as variables within it.
(i) USN
(ii) Name
(iii) Branch
(iv) Phone
Write a Java program to create nStudent objects and print the USN, Name, Branch, and Phone of these objects
with suitable headings.

CONCEPT

In Java, everything is encapsulated under classes. Class is the core of Java language. The Class can be defined as a
template/ blueprint that describes the behaviors /states of a particular entity. A class defines the new data type. This
type can be used to create an object of that type. The Object is an instance of the class. You may also call it as physical
existence of a logical template class.
A class is declared using class keyword. A class contains both data and code 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.

Object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior. An object reference provides a handle to an object that is created and stored in memory.

In Java, objects can only be manipulated via references, which can be stored in variables. Creating variables of your class type is similar to creating variables of primitive data types, such as integer or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. If you want to create an object of the class and have the reference variable associated with this object, you must also allocate memory for the object by using the new operator. This process is called instantiating an object or creating an object instance. In the following statement, obj is instance/object of Student class.

An array of objects is created just like an array of primitive type data items in the following way.

However, in this particular case, we may use for a loop since all Student objects are created with the same default constructor.

Constructor

The Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as a constructor.
Types of java constructors

There are two types of constructors:

1. Default constructor (no-arg constructor)
2. Parameterized constructor

A constructor that have no parameter is known as default constructor.

PROGRAM

OUTPUT

Creating a Java Class nStudent Output

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development.

You may also like...

2 Responses

  1. niraj bari says:

    Excellent and much useful post, especially for those who wants to start their java programming language. All these guidelines/tips are much useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: