aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiling and Importing Non-Static Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiling and Importing Non-Static Inner Class" Watch "Compiling and Importing Non-Static Inner Class" New topic
Author

Compiling and Importing Non-Static Inner Class

shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
I am reading Khalid Mughals's book for certification.
When I am trying to compile a program that imports inner classes (Pg 234(Example 7.5) Khalid Mughal's book) the compiler is giving error " package TLClassA does not exist".
Can someone tell me, if something is required for importing inner classes.
I have already executed example 7.4 and so all inner classes are present in the same folder in which I am running example 7.5.
For reference I am giving the codes also
******************************
//Example 7.5
//Filename ClientTLC2.java
class TLClassA{
private String msg = "TLClassA Object ";
public TLClassA(String objNo) {msg = msg + objNo;}
public void printmsg(){System.out.println(msg);}
class InnerB{
private String msg = "InnerB Object ";
public InnerB(String objNo) {msg = msg + objNo;}
public void printmsg(){System.out.println(msg);}
class InnerC{
private String msg = "InnerC Object ";
public InnerC(String objNo) {msg = msg + objNo;}
public void printmsg(){
System.out.println(msg);
System.out.println(this.msg);
System.out.println(InnerB.this.msg);
System.out.println(InnerC.this.msg);
InnerB.this.printmsg();
System.out.println(TLClassA.this.msg);
TLClassA.this.printmsg();
}
}
}
}
public class ClientTLC2{
public static void main(String args[]){
TLClassA a = new TLClassA("1");
TLClassA.InnerB b = a.new InnerB("1");
TLClassA.InnerB.InnerC c = b.new InnerC("1");
c.printmsg();
TLClassA.InnerB bb = new TLClassA("2").new InnerB("2");
TLClassA.InnerB.InnerC cc = bb.new InnerC("2");
cc.printmsg();
TLClassA.InnerB.InnerC ccc = new TLClassA("3").new InnerB("3").new InnerC("3");
ccc.printmsg();
}
}
*******************************************
//Example 7.5
//Filename ClientTLC3.java
import TLClassA.*;
public class ClientTLC3{
public static void main(String args[]){
TLClassA a = new TLClassA("1");
InnerB b = a.new InnerB("1");
InnerB.InnerC c = b.new InnerC("1");
c.printmsg();
InnerB bb = new TLClassA("2").new InnerB("2");
InnerB.InnerC cc = bb.new InnerC("2");
cc.printmsg();
InnerB.InnerC ccc = new TLClassA("3").new InnerB("3").new InnerC("3");
ccc.printmsg();
}
}package TLClassA does not exist


--Shweta<br />SCJP 1.4 <br />SCWCD
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi,
Why don't try CodeSaw
you can get it at www.codesaw.com. All the exemples of Mughal book are updated and can be compiled and run within it.
I just check the code it compiles fine.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Hi Shishio San !
Thank u for telling me abt this site... It seems to be an interesting site.
But still my problem is not solved. The example runs fine in CodeSaw but I want to know what is missing in my environment that I am getting the following error..
C:\Shweta\Java Programs\ClientTLC3.java:2: package TLClassA does not exist
import TLClassA.*;
^
C:\Shweta\Java Programs\ClientTLC3.java:7: cannot resolve symbol
symbol : class InnerB
location: class ClientTLC3
InnerB b = a.new InnerB("1");
^
C:\Shweta\Java Programs\ClientTLC3.java:8: package InnerB does not exist
InnerB.InnerC c = b.new InnerC("1");
^
C:\Shweta\Java Programs\ClientTLC3.java:10: cannot resolve symbol
symbol : class InnerB
location: class ClientTLC3
InnerB bb = new TLClassA("2").new InnerB("2");
^
C:\Shweta\Java Programs\ClientTLC3.java:11: package InnerB does not exist
InnerB.InnerC cc = bb.new InnerC("2");
^
C:\Shweta\Java Programs\ClientTLC3.java:13: package InnerB does not exist
InnerB.InnerC ccc = new TLClassA("3").new InnerB("3").new InnerC("3");
^
6 errors
Kedar Joshi
Greenhorn

Joined: Aug 26, 2002
Posts: 12
hi shweta!
I got same error you got!
I did not change the code, just executed following line
"set classpath=%classpath%;."
code worked fine! ... got it?!


Sing, laugh, dance, and meditate
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Kedar even after adding the . in my classpath I am getting the same error ...
Rather,I checked I already had that in my classpath !
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
shweta, I notice that both your classes are in the default package. Then you do not need any import statement.
when you write "import TLClassA.*", then a package by name TLClassA is looked for - remember that the package and class name can be the same.
You can get rid of the other compilation errors if you just prepend (if that is a word) TLClassA before each of the Inner class declarations in you client class.
I mean, you have to access the inner class as follows -
TLClassA.InnerB b = new TLClassA("outer").new InnerB("inner"
[ October 10, 2002: Message edited by: Vin Kris ]
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Thanks Kris,
What you said anyway is working.
But I actually was trying an example where I can import Inner classes , so that short names can be given inside the code.
My aim is to import inner classes, but compiler is assuming it to be a package and it so I am getting that error constantly.
I want to know that how can I tell the compiler what I am importing is a Nested class and not a package .. !!!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You can import inner classes ading its name to the name of its containing class:

If you omit line (1) , line (2) will not compile.
Just place the above class in the parent directory of the directory tlcclassa containing:

In your example the package sentence was missing.


SCJP2. Please Indent your code using UBB Code
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Hi Jose,
Thanks for ur help.Hey I tried what u said & it works fine.
In your code you have moved the class TLClassA in a package tlcclassa & made it public.
Then in the client program you have imported TLClassA by adding the package name before the class name.
This is working fine on my system !
I wonder what is the problem if we keep both the files in the default package. I guess the example which I referred (from KM's book) also keeps both the files in the default package.. !!!
In your example the package sentence was missing.

The package sentence is missing because I have kept both the files in the default package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling and Importing Non-Static Inner Class