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.

class Student{
String USN,name , branch;
int phoneno;
}

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.

Student obj=new Student();

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

Student[] obj_Array = new Student[7];

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

for ( int i=0; i<obj_Array.length; i++) {
obj_Array[i]=new Student();
}

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.

Student()
{
//block of code
}
Student obj=new Student();
Constructor with arguments is known as parameterized constructor.
Student(int i,String n){
id = i;
name = n;
}
Student4 s1 = new Student4(160,"RabinsXP");

PROGRAM

import java.util.Scanner;
import java.io.*;
public class student
{ String USN,name , branch;
int phoneno;
public void getinfo() throws Exception
{ InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter USN");
USN=br.readLine();
System.out.println("Enter Name");
name=br.readLine();
System.out.println("Enter Branch");
branch=br.readLine();
Scanner integer=new Scanner(System.in);
System.out.println("Enter phone number");
phoneno=integer.nextInt();
}
public void display()
{ System.out.printf("%s\t\t%s\t\t%s\t\t%d\n",USN,name,branch,phoneno);
}
public static void main(String[] args) throws Exception
{ int n,i;
Scanner integer=new Scanner(System.in);
System.out.println("Enter Number of student");
n=integer.nextInt();
//declare object with array
student[] obj=new student[n];
//object creation
for(i=0;i<n;i++)
{ obj[i]=new student();
System.out.printf("Student : %d\n",i+1);
obj[i].getinfo();
}
System.out.println("USN\t\tName\t\tBranch\t\tPhone Number");
for(i=0;i<n;i++)
obj[i].display();
}
}

OUTPUT

Creating a Java Class nStudent Output

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is senior ICT professional who talks about #it, #cloud, #servers, #software, and #innovation. Rabins is also the first initiator of Digital Nepal. Facebook: rabinsxp Instagram: rabinsxp

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 *