Justin Smith

Greenhorn
+ Follow
since Jul 24, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Justin Smith

Hi I have one doubt regarding Inner class... This is different than the other topic though... Can I create an Inner Class for a INNER class?? what will happen if I create and what is the use of it???

I tried creating it and was able to create one, but I wish to know is there any real world scenario which would need this kind of coding....


The sample example is given below:

import java.util.*;
import java.lang.*;

public class MyOuter implements java.io.Serializable
{
private int x = 6;
private String name = "Humpty Dumpty";
public MyOuter(){System.out.println("Calling the Outermost Constructor");}
public MyOuter(String arg){}
public double seeCurrent()
{
float f = 3.1416f;
Double d = Double.valueOf(f*x);
return d;
}
class MyInner
{
public MyInner(){System.out.println("Calling the MyInner Constructor");}
public void seeOuter()
{
System.out.println("The value of the 'x' is :"+x);
}
class MyInnerMost
{
public MyInnerMost(){System.out.println("Calling the MyInnerMost Constructor");}
public void seeOuterMost()
{
System.out.println("The 'name' selected for $1 million dollar is:"+name);
}
}
}

public static void main(String[] are) throws java.lang.Throwable
{
System.out.println("Before creating any instance in the main method");
Double value = null;
MyOuter mo = new MyOuter();
value = mo.seeCurrent();
System.out.println("The value of the return value is :"+value);
MyOuter.MyInner mi = new MyOuter().new MyInner();
mi.seeOuter();
MyOuter.MyInner.MyInnerMost mim = new MyOuter().new MyInner().new MyInnerMost();
mim.seeOuterMost();
}
}
16 years ago
Hi Baseet,

I tried creating one and was able to create it, but i wish to know is there any real world scenario which would need a scenario like this???

Here is my sample program....


import java.util.*;
import java.lang.*;

public class MyOuter implements java.io.Serializable
{
private int x = 6;
private String name = "Humpty Dumpty";
public MyOuter(){System.out.println("Calling the Outermost Constructor");}
public MyOuter(String arg){}
public double seeCurrent()
{
float f = 3.1416f;
Double d = Double.valueOf(f*x);
return d;
}
class MyInner
{
public MyInner(){System.out.println("Calling the MyInner Constructor");}
public void seeOuter()
{
System.out.println("The value of the 'x' is :"+x);
}
class MyInnerMost extends java.lang.Thread
{
public MyInnerMost(){System.out.println("Calling the MyInnerMost Constructor");}
public void seeOuterMost()
{
System.out.println("The 'name' selected for $1 million dollar is:"+name);
}
}
}

public static void main(String[] are)
{
System.out.println("Before creating any instance in the main method");
Double value = null;
MyOuter mo = new MyOuter();
value = mo.seeCurrent();
System.out.println("The value of the return value is :"+value);
MyOuter.MyInner mi = new MyOuter().new MyInner();
mi.seeOuter();
MyOuter.MyInner.MyInnerMost mim = new MyOuter().new MyInner().new MyInnerMost();
mim.seeOuterMost();
}
}

The output is :
---------------

The value of the return value is :18
Calling the Outermost Constructor
Calling the MyInner Constructor
The value of the 'x' is :6
Calling the Outermost Constructor
Calling the MyInner Constructor
Calling the MyInnerMost Constructor
The 'name' selected for $1 million d
Press any key to continue . . .
Hi I have one doubt regarding Inner class... This is different than the other topic though... Can I create an Inner Class for a INNER class?? what will happen if I create and what is the use of it???
One more question...

Can I create an Inner of Inner??? What will happen if I create one???

Hi Eric,

This is what i did....


-------------
abstract class InnerTest
{
protected InnerTest(){}
private int x =6;
public static void main(String[] gea)
{
System.out.println("Just here to say sorry i cannot instantiate InnerTest"); // won't get printed

}
abstract void myPieceofCake(String cake);
void howToGetToInner(String takeInner)
{
System.out.println("Am I Inside howToGetToInner(string) method???");
InnerMost im = new InnerMost();
im.seeOuter();
}
class InnerMost implements java.io.Serializable
{
public void seeOuter(){ System.out.println("The value of 'x'is:"+x); }
}

}

public class TestAbsInner extends InnerTest
{
void myPieceofCake(String ake)
{
System.out.println("Ofcourse it is my piece of cake");
}
void toTakeInner(String test)
{
System.out.println("To get the innermethod of an abstract class");
howToGetToInner(test);

}
public static void main(String[] gea)
{
TestAbsInner tai = new TestAbsInner();
String test= "JavaRanch";
tai.myPieceofCake(test);
tai.toTakeInner(test);
}
}
-----------------

And the output which I got is this:

Ofcourse it is my piece of cake
To get the innermethod of an abstract class
Am I Inside howToGetToInner(string) method???
The value of 'x'is:6

Hi kirba,

The line3 which is <Collection>containsAll(<Collection> is checking whether the given object has all the elements in another collection. If all the elements were present it is going to return true and if not it is going to return false. If you don't pass Collection in any of the comparison then you would get an compilation error.

Hope this clarifies your doubt..
Hi Fellowranchers....

I have a doubt regarding regular inner class(non-static,non-anonyms). Can we create a regular Inner class inside a abstract class??? If so what is the use of it???
Hi Chowdry,

Exception:
1) A programmer expects that inside his/her program that something might go wrong and tries to handle the exception if that happens
2) Exception can be of two types Checked and Unchekced and checked exception can be handled or declared in the program
3) You can pass the exception to the method which it called if the current method doesn't handle the exception. This is called ducking the exception to main().


Error:
1) Programmer doesn't know whether the error is going to happen or not say for example if you are reading a file from server and in case the server shut down these are called error.
2) You cannot handle or declare an error in you program.

Both Exception and error are from java.lang.Throwable class.

Hope this clarifies your doubt.
Hi Sutaria,

The reason why strings are not GCed is because, String will be handled in a different way than all the other Objects. Even though String IS-A Object, the memory handling of Strings are different. This is called String Pool. Before even creating a new String Literal say for example
"Sutaria" in the pool, it will check for the existance of the string. If it can't find one then it will create. So lets come your question

1) Even after the method returns, the string literal won't be GCed.

This is because the String pooling

2) If you create String s=null;
This will not create a String Object at all (unless you mention String s = "null" .This is because you have just given a reference 's' and not actually created an Object.

Hope this clarifies your doubt...
Hi Saravanan,

Regarding the one of the statement mentioned regarding Checked exception is where my doubt is

--You should compulsorily handle the checked exceptions in your code, otherwise your code will not be compiled. i.e you should put the code which may cause checked exception in try block. "checked" means they will be checked at compiletime itself.

I guess it should be reframed in this way

---Your code should handle (or) declare the checked exception.
Hi Raphael,

Remember, the order is:

1st. No Boxing or Var-args -> Primitive to Primitive / Wrapper to Wrapper
2nd. Autoboxing -> Primitive to Wrapper / Wrapper to Primitive
3rd. Var-args

In the 1st are you trying to say??

No Boxing or widening -> Primitive to primitive (int to long)
2nd and 3rd are correct...

Remember the order

Widening -> Boxing -> Var-args

Var args get the least priority.....
char's initial value is '\0'. This is nothing but character null.
From K&B Chapter 7: Page number 604:

One of the most common mistakes programmers make when creating
generic classes or methods is to use a <?> in the wildcard syntax rather than a type variable <T>, <E>, and so on. This code might look right, but isn�t:

public class NumberHolder<? extends Number> { }

While the question mark works when declaring a reference for a variable,
it does NOT work for generic class and method declarations. This code is not legal:

public class NumberHolder<?> { ? aNum; } // NO!

But if you replace the <?> with a legal identifier, you�re good:

public class NumberHolder<T> { T aNum; } // Yes


Hope this clarfies your doubt !!!
Thanks Ralph once again!!! But this time though for your prepartion tips......