Reputation: 11106
I have this code in a single file :
public class genIntro {
public static void main(String [] args){
genTest g = new genTest();
g.add( 10 );
System.out.println( g.get() == new Integer(10) ? true:false );
Integer in = (Integer) g.get();
}
}
class genTest(){
private Object object;
public void add(Object object) {
this.object = object;
}
public Object get() {
return object;
}
}
The second class genTest
has a wrong declaration seen with the brackets ()
.
In Netbeans 6.9.1 the code runs correctly and outputs false
.
Product Version: NetBeans IDE 6.9.1 (Build 201007282301)
Java: 1.6.0_21; Java HotSpot(TM) 64-Bit Server VM 17.0-b17
System: Windows 7 version 6.1 running on amd64; Cp1252; en_US (nb)
Userdir: C:\Users\Name\.netbeans\6.9
In Eclipse Indigo the code outputs :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at genIntro.main(genIntro.java:4)
Version: Indigo Service Release 1
Build id: 20110916-0149
Then manually compiling via the javac
command I get :
genIntro.java:12: '{' expected
class genTest(){
^
1 error
This is rather strange, can someone explain why the difference between them? Since it's erroneous why does it compile and run in Netbeans?
Running via javac genIntro.java
They all use jre6
Screenshot :
Upvotes: 4
Views: 436
Reputation: 80633
This is odd behaviour indeed. I created a new project in Netbeans (7.0.1) and added your code to it in a genIntro.java
file. Some observations:
Compiling 2 source files to /Users/tuoyo/Work/Data/Netbeans/Misc/build/classes /Users/tuoyo/Work/Data/Netbeans/Misc/src/misc/genIntro.java:14: '{' expected class genTest(){ 1 error /Users/tuoyo/Work/Data/Netbeans/Misc/nbproject/build-impl.xml:603: The following error occurred while executing this line: /Users/tuoyo/Work/Data/Netbeans/Misc/nbproject/build-impl.xml:245: Compile failed; see the compiler error output for details. BUILD FAILED (total time: 0 seconds)
Since Clean and Build invokes an Ant script behind the scenes I assume it is using the system JDK in console mode, which would jive with your original observations. Left unanswered is the question of how NetBeans is compiling the code when Run is selected - it does appear to be a different execution path.
Upvotes: 1
Reputation: 211
I tried it with javac 7 from the command-line and NetBeans 7.1, and it gives the same error as your javac example in both. Are you sure the source is the same in your netbeans version? I can't see how it would compile at all.
Changing the line "class genTest(){" to "class genTest {" allows it to compile, and prints 'false'.
Upvotes: 2