Solved

java programming

Posted on 2004-09-29
12
187 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hello,  sraveend.

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

Regards.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sraveend
Comment Utility
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
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
why did u accept that comment?
0
 

Author Comment

by:sraveend
Comment Utility
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
Comment Utility
thanks mate :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
thank you kindly :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now