Results 1 to 20 of 21
- 08-19-2013, 04:42 AM #1
Member
- Join Date
- Jul 2013
- Location
- india
- Posts
- 15
- Rep Power
- 0
check whether a object is interface or class
Java Code:public class CheckForInterface { public static void main(String[] args) throws ClassNotFoundException { Class class1=Class.forName("OneClass"); Class class2=Class.forName("MyInterface"); System.out.println(class1.isLocalClass()); System.out.println(class1.isInterface()); System.out.println(class2.isInterface()); } } interface MyInterface { } public class OneClass { }
The above code showing output
false
false
true
"OneClass" is a class still it is printing false..........then how can check it is a class or not..........is there any other code?
- 08-19-2013, 05:26 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: check whether a object is interface or class
That shouldn't even compile. OneClass should be in its own file since it is declare public.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-19-2013, 05:48 AM #3
Re: check whether a object is interface or class
Because class one is not a local class .
see Local Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 08-19-2013, 05:50 AM #4
Re: check whether a object is interface or class
right jim
- 08-19-2013, 07:12 AM #5
Member
- Join Date
- Jul 2013
- Location
- india
- Posts
- 15
- Rep Power
- 0
Re: check whether a object is interface or class
Java Code:public class CheckForInterface { public static void main(String[] args) throws ClassNotFoundException { Class class1=Class.forName("OneClass"); Class class2=Class.forName("MyInterface"); System.out.println(class1.isLocalClass()); System.out.println(class1.isInterface()); System.out.println(class2.isInterface()); } }
Java Code:interface MyInterface { }
Java Code:public class OneClass { }
all these classes and interface are in separate files..................how can i check the object class1,class2 is class type or interface type.............
- 08-19-2013, 09:06 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: check whether a object is interface or class
Everything in Java is a type; classes and interaces are types; if something isn't an interface it must be a class; the Class object can tell what sort of class it is: local, anonymous, member (or simply a top level class).
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-19-2013, 09:10 AM #7
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: check whether a object is interface or class
"OneClass" is a class still it is printing false..........then how can check it is a class or not
I must admit that I don't even know what a local class is. What I do know is that OneClass is not a local class.Last edited by gimbal2; 08-19-2013 at 09:13 AM.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-19-2013, 11:25 AM #8
Re: check whether a object is interface or class
I'm not terribly sure, but I think a local class is one that's declared inside a method, constructor or initializer.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-19-2013, 11:30 AM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: check whether a object is interface or class
I was guessing more towards that it is a standard JDK class or something. But your idea seems more plausible.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-19-2013, 02:07 PM #10
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: check whether a object is interface or class
I did some testing on this. I find the tests rely on the actual .class name which includes either a $ or a $ followed by a digit in the name.
Java Code:package examples; public class ClassTest { public static void main(String[] args) throws ClassNotFoundException{ // TODO Auto-generated method stub new ClassTest().startUp(); } public void startUp() throws ClassNotFoundException { Class<?> firstLocalClass = foo(1).getClass(); Class<?> secondLocalClass = foo(2).getClass(); Class<?> firstLocalClass1 = foo1(1).getClass(); Class<?> secondLocalClass1 = foo1(2).getClass(); Class<?> thirdLocalClass1 = foo1(3).getClass(); Class<?> memberClass = (this.new MemberClass()).getClass(); Class<?> interfaceType = Class.forName("examples.Interface"); System.out.println(firstLocalClass.getName() + " = " + firstLocalClass.isLocalClass()); System.out.println(secondLocalClass.getName() + " = " +secondLocalClass.isLocalClass()); System.out.println(firstLocalClass1.getName() + " = " + firstLocalClass1.isLocalClass()); System.out.println(secondLocalClass1.getName() + " = " +secondLocalClass1.isLocalClass()); System.out.println(thirdLocalClass1.getName() + " = " +thirdLocalClass1.isLocalClass()); System.out.println(memberClass.getName() + " = " +memberClass.isMemberClass()); System.out.println(interfaceType.getName() + " = " +interfaceType.isInterface()); System.out.println("Testing for anonymous classes"); ClassTest anon1 = new ClassTest() {}; ClassTest anon2 = new ClassTest() {}; Class<?> anon1Class = anon1.getClass(); Class<?> anon2Class = anon2.getClass(); System.out.println(anon1Class.getName() + " = " + anon1Class.isAnonymousClass()); System.out.println(anon2Class.getName() + " = " + anon2Class.isAnonymousClass()); } public Object foo(int which) { class FirstLocalClass {} class SecondLocalClass {} switch (which) { case 1: return new FirstLocalClass(); case 2: return new SecondLocalClass(); } return null; } public Object foo1(int which) { class FirstLocalClass {} class SecondLocalClass {} class ThirdLocalClass {} switch (which) { case 1: return new FirstLocalClass(); case 2: return new SecondLocalClass(); case 3: return new ThirdLocalClass(); } return null; } public class MemberClass{ MemberClass(){} } } interface Interface {}
examples.ClassTest$1FirstLocalClass = true
examples.ClassTest$1SecondLocalClass = true
examples.ClassTest$2FirstLocalClass = true
examples.ClassTest$2SecondLocalClass = true
examples.ClassTest$1ThirdLocalClass = true
examples.ClassTest$MemberClass = true
examples.Interface = true
Testing for anonymous classes
examples.ClassTest$1 = true
examples.ClassTest$2 = true
A couple observations:
1. Local class names have a digit after the $. It only changes when the names are duplicated in other methods.
2. I could not seem to get an instantiated designation of an interface to return true. I had to specify the name in Class.forName. All instantiations or assignments of interfaces are reported as false for isInterface().
3. I didn't do an exhaustive test for false reports. But what I did test above reported false when applied to other type checks.
Edit: Idiot! I left out the foo1() method and calls. At least now the code matches the output. Shouldn't affect any previous posts.
Regards,
JimLast edited by jim829; 08-19-2013 at 11:39 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-19-2013, 02:17 PM #11
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 67
- Rep Power
- 0
Re: check whether a object is interface or class
public class OneClass {
}
- 08-19-2013, 04:54 PM #12
Member
- Join Date
- Jul 2013
- Location
- india
- Posts
- 15
- Rep Power
- 0
Re: check whether a object is interface or class
i got it thanx.......
- 08-19-2013, 07:59 PM #13
Re: check whether a object is interface or class
- 08-19-2013, 08:05 PM #14
- 08-19-2013, 08:20 PM #15
- 08-19-2013, 09:31 PM #16
- 08-19-2013, 11:54 PM #17
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: check whether a object is interface or class
I would say not. Since isSynthetic returns false for anonymous classes. The JLS says this:
Chapter*13.*Binary Compatibility
"7. Any constructs introduced by a Java compiler that do not have a corresponding construct in the source code must be marked as synthetic, except for default constructors, the class initialization method, and the values and valueOf methods of the Enum class."
I had never heard of any of these terms until this thread.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-20-2013, 12:59 PM #18
- 08-20-2013, 01:04 PM #19
Re: check whether a object is interface or class
Can't understand why is this
Java Code:Class<?> anon1Class = anon1.getClass();
- 08-20-2013, 03:03 PM #20
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: check whether a object is interface or class
No because anon1Class is an instance of an anonymous class.
Java Code:Class Foo { public void cheer() { System.out.println("Rah!"); } } Foo f1 = new Foo(); // regular instance, not anonymous Foo f2 = new Foo() { // creating anonymous instance public void cheer() { System.out.println("Ole!"); } };
This is also anonymous (doesn't override anything but note the syntax).
Java Code:Foo f3 = new Foo() {};
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Interface Inheritance : create class that implements the interface
By Zulkifli_Rahman in forum New To JavaReplies: 1Last Post: 07-08-2012, 05:23 PM -
object check
By droidus in forum New To JavaReplies: 7Last Post: 12-08-2011, 02:39 AM -
How to return object or class from functions using Java Native Interface
By shai1981 in forum Advanced JavaReplies: 0Last Post: 10-03-2011, 08:27 AM -
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
create an object of interface
By yma16 in forum New To JavaReplies: 6Last Post: 04-16-2011, 04:28 AM
Bookmarks