Home arrow static arrow Java Programming [Archive] - Java Compiler Issues
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java Compiler Issues
This topic has 3 replies on 1 page.

Posts:10
Registered: 10/14/03
Java Compiler Issues  
Jun 25, 2004 5:44 AM



 
If the following pgm is executed the output is---
This is Class C
This is Class C
This is Class B

class C
{
//base class
C()
{
System.out.println("This is Class C");
}
}

class B extends C
{
//derived class
B()
{
System.out.println("This is Class B");
}
}

class A extends C
{
public static void main(String args[])
{
C cTempA = new A();
C cTempB = new B();
}
}

However, if the Class B is commented (as below) and the code is recomplied, the Output Remains the same. This is because the B.class file still exists. But should the compiler not fire an error for the statement C cTempB = new B(); ???

class C
{
//base class
C()
{
System.out.println("This is Class C");
}
}

// Class B is commented
/* class B extends C
{
//derived class
B()
{
System.out.println("This is Class B");
}
} */

class A extends C
{
public static void main(String args[])
{
C cTempA = new A();
C cTempB = new B();
}
}
 

Posts:1,385
Registered: 10.06.03
Re: Java Compiler Issues  
Jun 25, 2004 5:48 AM (reply 1 of 3)



 
However, if the Class B is commented (as below) and
the code is recomplied, the Output Remains the same.
This is because the B.class file still exists. But

Yes, exactly.

should the compiler not fire an error for the
statement C cTempB = new B(); ???

No. You've already answered your own question. Class B still exists (in the file B.class), so there's no problem. When you compile your source code, the compiler finds the file B.class.
 

Posts:10
Registered: 10/14/03
Re: Java Compiler Issues  
Jun 25, 2004 5:53 AM (reply 2 of 3)



 
Thats right, but isn't the compilers job to create a .class file? Why is it searching for class file at compile time????
 

Posts:3,055
Registered: 18/06/98
Re: Java Compiler Issues  
Jun 25, 2004 6:07 AM (reply 3 of 3)



 
Because searching for classes that are missing in the source code ( but available as .class files ), is also the compiler's job...

From my old C/C++ times I've learned that computers are getting faster and rebuilding all your application (cleaning old .class files) does not hurt anyone; it avoids me a lot of headaches. It's like keeping your teeth clean.

(Well, recompiling all the J2SE SDK - source code has about 167 MB - or all the J2EE SDK - zipped source code has about 560MB - , or rebuilding a Linux kernel, or rebuilding the Mozilla browser, is not my everyday's task).
 
This topic has 3 replies on 1 page.