Aryan Venkat

Greenhorn
+ Follow
since Aug 18, 2012
Aryan likes ...
Eclipse IDE Oracle Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Aryan Venkat

Thank you very much for your awesome reference, I'll be grateful to you and I just wish to see me myself like you, may be after any long time, even decades and decades, I'll try hard to achieve that knowledge. I feel it's a great day for me today, I'll start with 'that' end (capability) in my mind, now with a little knowledge.
11 years ago
@Winston,Sorry Sorry Winston , if I hurt you in any way; I'm just in a feel and hurry to learn more as java greenhorn, I feel I need the help of experts like you. It's just that.

Finally, thanks a lot for your time and interest, thanks for everything. you don't need to reply for this.
11 years ago
Thank you Winston , but I just want dig deeper, any inner class, either it may be a member class or static nested or local/anonymous anything, but I need why and in which cases we've to opt for Inner classes, apart from the usage, they provide more encapsulation and security...Help me please.
11 years ago
Oh I understood Mr. Winston,but I feel I was not up to the mark, can you please offer me any REAL-TIME scenario with an instance, that illustrates why use inner classes much?
11 years ago
I conform to Pat.Believe it or not, there are advantages to Java's inner classes. But before we go into that, I'll provide a short background on inner classes.

Inner classes nest within other classes. A normal class is a direct member of a package, a top-level class. Inner classes, which became available with Java 1.1, come in four flavors:

Static member classes
Member classes
Local classes
Anonymous classes


An Inner class is a nested one, either it may be defined inside a class or inside a method, sometimes, can be anonymously.

Inner classes allow us to achieve more encapsulation and thus more security.

1.Member Class
----------------
Here, Inner class is termed as Member class.

class Outer
{
class Inner
{

}
}

*Creating the object of Inner class needs an object of outer class.

new Outer().new Inner();

To hold the object of the Inner class, the reference must be of the form,

Outer.Inner obj;


Finally,

Outer.Inner obj=new Outer().new Inner();

We can have either way,

Outer out=new Outer();
Outer.Inner obj=out.new Inner();


*An Inner class can have access to Outer class members, but the reverse is not true.

*An Inner class is just like a member of a class, and allowed all the modifiers like public,private,protected,default,static,final,abstract etc.



2.Static Inner Class
=====================

* Creation of an object of the Static Inner class doesn’t require an Outer class object, just requires the name of the Outer class.

new Outer.StaticInner();

To hold the object of the Inner class, the reference must be of the form,

Outer.StaticInner obj;


Finally,

Outer.StaticInner obj=new Outer.StaticInner();

No other way.


3.Local Inner Class
=====================

A class defined inside the body of a method.

It’s like local member of a method, can’t have access specifiers like public,private,protected.

Can have only final,abstract.

We can directly execute the Local Inner class by calling the enclosing function.

If we want any method written inside the Local Inner class, we’ll call that method, using an object created to that Local class, in the enclosing function.

We can’t have static data inside the Local Inner class.

We can’t access a non-final variable (of the enclosing function) inside a Local Inner class method.



Example:
--------

//A Static Inner class Example
class Outer
{
void enclosingFun()
{
System.out.println("Hello Outer fun");

class Local
{

void localFun()
{
System.out.println("I'm Local");
}

}
Local l=new Local();
l.localFun();
}
static class StaticInner
{
void display()
{
System.out.println("Hello Static Inner fun");

}

}

}

public class LocalInnerDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Outer o=new Outer3();
o.enclosingFun();

}
}

4. Anonymous class
==================

A class defined inside the body of a method; and is of no name.

Example
========
//A Static Inner class Example
class Outer4
{
void display()
{
System.out.println("Displayed in Outer class");

}


}

public class AnonymousDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Outer4 o=new Outer4(){

void display()
{

System.out.println("Displayed in Anonymous class");
super.display();
}

};

o.display();

}

}
11 years ago
Nice answer Mr.Stuart,
I need one more clarification.


Java comes along with the virtual machine JVM and the API, in a similar way, C proramming language comes along with the Compiler,Linker and others which run the C program(considering Turbo C), and it has the standard library, then why can't we call C as a technology???

Please help me.
11 years ago