Solved

java programming

Posted on 2004-09-30
10
224 Views
Last Modified: 2010-07-27
Please give me difference between class and Class
Can i store non-primitive type of a field ,class in a class?  
0
Comment
Question by:sraveend
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12196975
> Please give me difference between class and Class

what exactly are you referring to?

> Can i store non-primitive type of a field ,class in a class?

yes

public class A
{
   private B field;
}
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12196980
class it can be variable name.

0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 125 total points
ID: 12196988
0
 
LVL 8

Expert Comment

by:thomas908
ID: 12197388
class is a keyword used to declare a java class.
Class is a inbuild class in java.lang package
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12197427
class  is used in two places
either declaring a class
like
public class A
{
   private B field;
}

or it can be used to get the class object of an object :)
like this
obj.class.getName();

Class is a class in java.lang package
"Instances of the class Class represent classes and interfaces in a running Java application"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:Giant2
ID: 12197573
>class it can be variable name.

NO. It's a java keyword and indicate to declare a class.

Class (with C uppercase) is a particular class used into java program. See here the doc:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12197590
>class it can be variable name.
wrong comment from me apologize for tht
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12197596
>>Can i store non-primitive type of a field ,class in a class?  

Alternatively, if you mean this, then you can

public class A {
    private Class clazz;
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12197649
The member variables (fields) can be of type primitives, or a particular class.
The class java.lang.Class is no different to any other class in this respect.

What the class java.lang.Class provides is details about a particula java class.
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 125 total points
ID: 12198531
Everything in Java is built out of classes (except for primatives).  So a class is just how you build objects in Java.  In otherwords a class is just a synonym for object.

A Class is a special type of object that is used by Java's reflection tools.  An instance of Class contains all of the definition of the object.  It allows you do look at the object at runtime.  For example
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Foo
{
   public int a,b,c;
   private double x,y,z;
   public void bar() throws Exception
   {
            Class me=Foo.class;
            //Another way to get this would be:
         //Class me=Class.forName("Foo");

         System.out.println("class "+me.getName());
         Field[] f=me.getFields();
         for (int i=0;i<f.length;i++)
               System.out.println("field "+f[i].getName());
         Method[] m=me.getMethods();
         for (int i=0;i<m.length;i++)
               System.out.println("method "+m[i].getName());
       
         Foo meInstance=(Foo) me.newInstance();   //This is the same as Foo meInstance=new Foo();

   }

   public static void main(String[] args)
   {
        Foo meInstance=new Foo();
        try {
            meInstance.bar();
      } catch (Exception e) {
            e.printStackTrace();
      }
   }
}

Run the above code and see for yourself what it outputs
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

863 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

23 Experts available now in Live!

Get 1:1 Help Now