aspose file tools*
The moose likes Java in General and the fly likes Analyze this code:  instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Analyze this code:  instanceof" Watch "Analyze this code:  instanceof" New topic
Author

Analyze this code: instanceof

Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Can anyone point out what is wrong with this code:

class A {

A(){
}

}

class B extends A {

B(){
}


}



class TestInstanceof {

public static void main(String a[]){

A a = new A();
B b = new B();

if(a instanceof b){

System.out.println("A is instance of B");

} else {

System.out.println("A is not instance of B");

}

if(b instanceof a){

System.out.println("B is instance of A");

} else {

System.out.println("B is not instance of A");

}



}



}

I get a compliation error:

C:\TestInstanceof.java:26: a is already defined in main(java.lang.String[])
A a = new A();
^
C:\TestInstanceof.java:29: cannot resolve symbol
symbol : class b
location: class TestInstanceof
if(a instanceof b){
^
C:\TestInstanceof.java:39: cannot resolve symbol
symbol : class a
location: class TestInstanceof
if(b instanceof a){
^
3 errors

Tool completed with exit code 1
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

C:\TestInstanceof.java:26: a is already defined in main(java.lang.String[])
A a = new A();


Well, the error message says it all. You already have something else called "a". Look closely at the first line of your main() method:

public static void main(String a[]){

Can you see that you already have a variable named "a" there?

About the other errors:

1. Java is case-sensitive. So "b" is not the same as "B".
2. "instanceof" does not take two objects as parameters. It takes an object and a class name - that's why the compiler says it cannot find class b or class a.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Thanks buddy, got the code running..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Analyze this code: instanceof