Java Assignment
Student Name: Gajendra Gangwar
Registration Number: 11810542
Section: K18KY
Roll No: 26
[Link] 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()
{
[Link]("outer_x = " + outer_x);
[Link]("outer_private = " + outer_private);
}
}
}
public class StaticNestedClassDemo
{
public static void main(String[] args)
{
[Link] = new [Link]();
[Link]();
}
}
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()
{
[Link]("outer_x = " + outer_x);
[Link]("outer_y = " + outer_y);
[Link]("outer_private = " + outer_private);
}
}
}
public class InnerClassDemo
{
public static void main(String[] args)
{
OuterClass outerObject = new OuterClass();
[Link] innerObject = [Link] InnerClass();
[Link]();
}
}
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 [Link];
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();
[Link] = name;
[Link] = percentage;
[Link] = dob;
StudentMarks.reg_id=reg_id;
public StudentMarks(String name, double percentage, String dob) {
super();
[Link] = name;
[Link] = percentage;
[Link] = dob;
public StudentMarks()
reg_id++;
public void printDetails()
[Link]();
[Link]("Name: "+name);
[Link]("Percentage: "+percentage);
[Link]("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;
[Link] = sec;
[Link]=studentMarks;
public void printCard()
{
[Link]("------------Student Card------------");
[Link]("Name: "+[Link]);
[Link]("Percentage:"+ [Link]+"
"+"DOB: "+[Link]);
[Link]("Section:"+ sec+" "+"Roll
No:"+roll_no+" ");
public static void main(String[] args)
Scanner scanner=new Scanner([Link]);
[Link]("Enter no of students");
int n=[Link]();
for (int i = 0; i < n; i++) {
[Link]();
[Link]("Enter name");
String name2=[Link]();
[Link]("Enter percentage");
double percentage2=[Link]();
[Link]("Enter Date of Birth :format: dd/mm/yyyy");
String dob2=[Link]();
if(percentage2>=65)
[Link]("Enter Section:");
String secString=[Link]();
[Link]("Enter Roll No:");
int roll_no=[Link]();
StudentMarks studentMarks=new StudentMarks(name2,
percentage2,dob2,++reg_id);
[Link] sc=new StudentMarks().new
StudentClass(roll_no, secString,studentMarks);
[Link]();
else {
StudentMarks studentMarks2=new StudentMarks(name2, percentage2,
dob2);
[Link]();
[Link]();
3.