Solved

java programming

Posted on 2004-09-29
12
188 Views
Last Modified: 2010-03-31
I came to know that

Class aClass = Class.forName("A");  
(or)
 aClass = A.class;


loads a class to the reference object variable  aclass.Is the above statement similar to
class A
{
}
A  aclass=new A();

 
0
Comment
Question by:sraveend
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12182026
Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

The following example uses a Class object to print the class name of an object:

     void printClassName(Object obj) {
         System.out.println("The class of " + obj +
                            " is " + obj.getClass().getName());
     }
 
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 400 total points
ID: 12182096
Using Class class you can identify and methods of particular class
here are some examples
=================================================
   import java.lang.reflect.*;
 
   public class DumpMethods {
      public static void main(String args[])
      {
         try {
            Class c = Class.forName(args[0]);
            Method m[] = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++)
            System.out.println(m[i].toString());
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }


=================================================
 class A {}

   public class instance1 {
      public static void main(String args[])
      {
         try {
            Class cls = Class.forName("A");
            boolean b1
              = cls.isInstance(new Integer(37));
            System.out.println(b1);
            boolean b2 = cls.isInstance(new A());
            System.out.println(b2);
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }

=================================================
   import java.lang.reflect.*;

   public class method1 {
      private int f1(
       Object p, int x) throws NullPointerException
      {
         if (p == null)
            throw new NullPointerException();
         return x;
      }
       
      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("method1");
       
            Method methlist[]
              = cls.getDeclaredMethods();
            for (int i = 0; i < methlist.length;
               i++) {  
               Method m = methlist[i];
               System.out.println("name
                 = " + m.getName());
               System.out.println("decl class = " +
                              m.getDeclaringClass());
               Class pvec[] = m.getParameterTypes();
               for (int j = 0; j < pvec.length; j++)
                  System.out.println("
                   param #" + j + " " + pvec[j]);
               Class evec[] = m.getExceptionTypes();
               for (int j = 0; j < evec.length; j++)
                  System.out.println("exc #" + j
                    + " " + evec[j]);
               System.out.println("return type = " +
                                  m.getReturnType());
               System.out.println("-----");
            }
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }


=================================================

  import java.lang.reflect.*;
       
   public class method2 {
      public int add(int a, int b)
      {
         return a + b;
      }
       
      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("method2");
           Class partypes[] = new Class[2];
            partypes[0] = Integer.TYPE;
            partypes[1] = Integer.TYPE;
            Method meth = cls.getMethod(
              "add", partypes);
            method2 methobj = new method2();
            Object arglist[] = new Object[2];
            arglist[0] = new Integer(37);
            arglist[1] = new Integer(47);
            Object retobj
              = meth.invoke(methobj, arglist);
            Integer retval = (Integer)retobj;
            System.out.println(retval.intValue());
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }
0
 
LVL 2

Expert Comment

by:SergeiKo
ID: 12183452
Hello,  sraveend.

Also, look at the question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21141295.html

Regards.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 92

Expert Comment

by:objects
ID: 12184855
> loads a class to the reference object variable  aclass.Is the above statement similar to

No,
the first creates an insance of the class Class.
the second creates an instance of the class A.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12186833
even these two lines are not alike
Class aClass = Class.forName("A");  
(or)
aClass = A.class;

Class.forName("A");  which is a slower process than
A.class;
0
 

Author Comment

by:sraveend
ID: 12190683
Could you suggest me a website which contains  tutorials related to Reflection in java .lang
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 12194537
0
 
LVL 92

Expert Comment

by:objects
ID: 12194543
why did u accept that comment?
0
 

Author Comment

by:sraveend
ID: 12196399
This is the first time am using this website,there was a small mistake,THanks a lot for giving me very good solution!
sudhakar koundinya solution was very,very usefull for me!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12208404
thanks mate :)
0
 
LVL 92

Expert Comment

by:objects
ID: 12209220
thank you kindly :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 103
micro services spring boot application error 3 185
Java Jpanels and Jframe 8 32
runtime exception 2 27
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question