You are on page 1of 13

Nested classes

Putting the definition of one class inside the definition of


another class is called nesting the class. the inside class is
called nested class. A nested class can itself have another
class nested inside it.

A nested class can have an access attribute just like other


class members and its accessibility outside the enclosing
class determined by the attribute in the same way.
Nested classes

 Public class a
 {
 //nested class
 Public class b
 {
 //detail of b class
 }
 // more members of a class
 }
Nested classes

 A nested class have some specific association with enclosing


class. The enclosing class here referred to as top level class. A
top level class contains the nested class but it not itself a nested
class.
 if a nested class is not a static member of top level class then
you can’t create the object of nested class until the object of top
level class has been created. e.g
suppose you have create the object of toplevel class
a a1 = new a();
now if you wish to create the object of nested class
you can create it as follows
a.b b1 = a1.new b();
 All this implies because the static method can not create the
object of non static nested class.
Nested classes

 Suppose if you create the object of non static


nested class within the non static methods
that are members of top level class then you
can create the object of nested class simply
as
b b1 = new b();
which is equivalent to
this.b b1 = this.new b();
Static nested classes
 To make a object of nested class type independent of object of top level class, you can
declare the nested class as static.
e.g
public class a
{
public static class b
{
// detail of class b;
}
public class c
{
// detail of class c;
}
//more member of top level class
}
object of static nested class
a.b b1= new a.b();
Member of A static nested class can only access the static members of top level class.
Non static nested class

 A non static nested class often refer to as a


inner class.
 A non static nested class can not contain the
static data members it self. But it can access
any member of the top level class as well as
it can access the static data members of
other static nested classes within the same
top level class .
Nested classes

 Often there is a tight coupling b/w the nested


and top level class. Nested classes are
typically used to define the objects that have
strong association with the object of top level
class.
 An other use of nested classes is to grouping
the related classes within the umbrella of
enclosing class.
Local nested class.
 You can define a class within the body of a
method – where it is called local nested class
it also referred to as local inner class.
 Objects of local inner class will also be a
local that mean’s its objects will be created
within the same method in which the class
definition appear.
 A local inner class can refer to the variables
of the method in which it is defined but only if
they are final.
Packages
How to create Packages.
four steps to create packages.
 A class which you want to include in your package

should be declared as public.


 Choose package name & add package statement to

the source code file.


 Compile the file so it is placed in appropriate

directory.
 Import the reusable class in to your program and

use it.
example
 package mcs.third.morning;
 import java.io.*;
 public class msg
 {
 msg()
 {

 }
 public static void print()
 {
 System.out.println(“I m a first class of mcs package");
 }

 }
 Save the file with the class name e.g msg.java
 Compile this file using the following command.

javac –d . msg.java
Now you can import this package in your classes. E.g

 import mcs.third.morning.msg;
 public class testpackage
 {
 public static void main(String a[])
 {
 msg.print();
 }
 }
String Functions.

1. StringName.CharAt(0);
2. StringName.indexOf(‘A’);
3. StringName.lastindexOf(‘a’);
4. StringName.endsWith(“ing”);
5. StringName.startsWith(“wel”);
6. StringName.equals(StringName);
7. StringName.equalsIgnoreCase(StringName);
8. StringName.toLowercase();
9. StringName.toUpperCase();
10.StringName.=StringName.replace(‘s’,’y’);
String functions.

11.StringName.length();
12.StringName.Concate(“new string”)
13.StringName.subString(0,10);
14.StringName.trim();
15.StringName=String.copyValueOf(array);
16.Array=StringName.toCharArray();

You might also like