Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java programming

Posted on 2004-09-29
12
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

718 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