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
Solved

java programming

Posted on 2004-09-29
12
189 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 42
How to determine if a string is a valid SHA value 7 35
Java Eclipse Loop 3 20
Setup GlassFish 4 21
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

790 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