Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree4Likes

Thread: check whether a object is interface or class

  1. #1
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Post 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?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class


  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class

    right jim

  5. #5
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Default 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.............

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    rajeev likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default 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
    Its not an interface, so if you ask it "are you an interface" it is going to reply FALSE! Or perhaps you were referring to the isLocalClass() call?

    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

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default 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

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default 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 {}
    When run, the following output was produced:

    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,
    Jim
    Last edited by jim829; 08-19-2013 at 11:39 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: check whether a object is interface or class

    public class OneClass {

    }
    i have small doubt in the ave program that is , is it possible to define two public classes in a program? in the above program there are two public classes one is OneClass and another one is CheckForInterface

  12. #12
    rajeev is offline Member
    Join Date
    Jul 2013
    Location
    india
    Posts
    15
    Rep Power
    0

    Default Re: check whether a object is interface or class

    i got it thanx.......

  13. #13
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class

    Quote Originally Posted by chandrasekhar.melam View Post
    i have small doubt in the ave program that is , is it possible to define two public classes in a program? in the above program there are two public classes one is OneClass and another one is CheckForInterface
    Rajeev already said that "all these classes and interface are in separate files". and yes it is possible to define two public classes in a programe but defining two public classes in a single source file is not possible ...

  14. #14
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class

    Quote Originally Posted by jim829 View Post
    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.

    When run, the following output was produced:

    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.
    Are these synthetic classes ? I don't know the exact definition but I think their names have $ sign
    Villa likes this.

  15. #15
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class

    Quote Originally Posted by jim829 View Post
    Java Code:
         
          
          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());
       }
    How are these anonymous classes ? I read that anonymous classes are those which donot hava names ...

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: check whether a object is interface or class

    Quote Originally Posted by allaudin View Post
    How are these anonymous classes ? I read that anonymous classes are those which donot hava names ...
    Try running this line of code:
    Java Code:
    System.out.println(new Object() {}.getClass().getName());
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default 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."
    So I would equate a synthetic class as similar to a regular class as a bridge method is to a regular method within generics. The compiler generates bridge methods to help with wildcard capture situations, yet they don't appear in the code. I have never seen a synthetic class nor can I give an example of how one is manifested.

    I had never heard of any of these terms until this thread.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class

    Quote Originally Posted by DarrylBurke View Post
    Try running this line of code:
    Java Code:
    System.out.println(new Object() {}.getClass().getName());
    db
    you mean $(digit) is for anonymous and local classes ?

  19. #19
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: check whether a object is interface or class

    Can't understand why is this
    Java Code:
     Class<?> anon1Class = anon1.getClass();
    anonymouse .. it shouldn't be ClassTest ????

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default 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!");
           }
     };
    f2 is anonymous. What is the name of the instance which overrides method()?

    This is also anonymous (doesn't override anything but note the syntax).
    Java Code:
    Foo f3 = new Foo() {};
    Regards,
    Jim
    allaudin likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  2. object check
    By droidus in forum New To Java
    Replies: 7
    Last Post: 12-08-2011, 02:39 AM
  3. Replies: 0
    Last Post: 10-03-2011, 08:27 AM
  4. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  5. create an object of interface
    By yma16 in forum New To Java
    Replies: 6
    Last Post: 04-16-2011, 04:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •