Java Assignment
Student Name: Gajendra Gangwar
Registration Number: 11810542
Section: K18KY
Roll No: 26
1.Definition of nested class
A class defined within another class is called a nested class. The Scope of a nested class
is within the scope of its enclosing class. That is, if class B is enclosed in class A,
then B in known only to A. class B can accessed the members of class A whereas class A
does not have access to members of class B.
Syntax:
Class outerclass
...
Class NestedClass
...
1.1 Types of Nested classes
1.1.1 Static nested class: A static nested class has a static modified preceding its
declaration.
It cannot access the members of its enclosing class directly. It has to use object of
enclosing class.
class OuterClass
{
static int outer_x = 20;
int outer_y = 30;
private static int outer_private = 40;
static class StaticNestedClass
{
void display()
{
System.out.println("outer_x = " + outer_x);
System.out.println("outer_private = " + outer_private);
}
}
}
public class StaticNestedClassDemo
{
public static void main(String[] args)
{
OuterClass.StaticNestedClass = new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
1.1.2 Non-static class: A non-static nested class is called as inner class. It can directly
access all the variables and methods of its outer class.
1.2 There are two special kinds of inner classes:
1.2.1 Local inner classes
1.2.2 Anonymous inner classes
class OuterClass
{
static int outer_x = 20;
int outer_y = 20;
private int outer_private = 40;
class InnerClass
{
void display()
{
System.out.println("outer_x = " + outer_x);
System.out.println("outer_y = " + outer_y);
System.out.println("outer_private = " + outer_private);
}
}
}
public class InnerClassDemo
{
public static void main(String[] args)
{
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
1.3 Importance of nested classes
➢ Nested classes represent a special type of relationship that is it can access all the members
(data members and methods) of outer class including private.
➢ Nested classes are used to develop more readable and maintainable code because it logically
group classes and interfaces in one place only.
➢ Code Optimization: It requires less code to write.
1.4 Uses of Nested class
➢ It is a way of logically grouping classes that are only used in one place.
➢ It increases encapsulation.
➢ Nested classes can lead to more readable and maintainable code
➢ Child to parent class connection is simpler as it visually Illustrate the variables and methods
of each class.
2. Write a program to create a class Student which contains attributesname,
date_of_birth, marks,percentage and Reg_id. Provide appropriate
constructor to initialize all the attributes of the Student but Reg_id
must be assigned automatically only when the marks of the student is greater
than or equal to 65%. StudentClass is a nested class with attributes roll_no
and Section_name. Make sure that Student card is created only when user
is avalid and if it is already created then must not be assigned the new
roll number.
import java.util.Scanner;
public class StudentMarks {
String name;
double percentage;
String dob;
static int reg_id=10000;;
public StudentMarks(String name, double percentage, String dob,int reg_id) {
super();
this.name = name;
this.percentage = percentage;
this.dob = dob;
StudentMarks.reg_id=reg_id;
public StudentMarks(String name, double percentage, String dob) {
super();
this.name = name;
this.percentage = percentage;
this.dob = dob;
public StudentMarks()
reg_id++;
public void printDetails()
System.out.println();
System.out.println("Name: "+name);
System.out.println("Percentage: "+percentage);
System.out.println("DOB: "+dob);
class StudentClass{
int roll_no;
String sec;
StudentMarks studentMarks;
public StudentClass(int roll_no, String sec,StudentMarks studentMarks) {
super();
this.roll_no = roll_no;
this.sec = sec;
this.studentMarks=studentMarks;
public void printCard()
{
System.out.println("------------Student Card------------");
System.out.println("Name: "+studentMarks.name);
System.out.println("Percentage:"+ studentMarks.percentage+"
"+"DOB: "+studentMarks.dob);
System.out.println("Section:"+ sec+" "+"Roll
No:"+roll_no+" ");
public static void main(String[] args)
Scanner scanner=new Scanner(System.in);
System.out.println("Enter no of students");
int n=scanner.nextInt();
for (int i = 0; i < n; i++) {
System.out.println();
System.out.println("Enter name");
String name2=scanner.next();
System.out.println("Enter percentage");
double percentage2=scanner.nextDouble();
System.out.println("Enter Date of Birth :format: dd/mm/yyyy");
String dob2=scanner.next();
if(percentage2>=65)
System.out.println("Enter Section:");
String secString=scanner.next();
System.out.println("Enter Roll No:");
int roll_no=scanner.nextInt();
StudentMarks studentMarks=new StudentMarks(name2,
percentage2,dob2,++reg_id);
StudentMarks.StudentClass sc=new StudentMarks().new
StudentClass(roll_no, secString,studentMarks);
sc.printCard();
else {
StudentMarks studentMarks2=new StudentMarks(name2, percentage2,
dob2);
studentMarks2.printDetails();
scanner.close();
3.