Class Class in Java: Know All-inclusive Just In 20 Minutes

Class Class in Java

Introduction to Class Class in Java

Say we want to design a beautiful city. For that, we need to chalk out a plan for roads, houses, shops, etc in a paper.

Then we will design each of these individually, say a house. The blueprint of the house will say how many doors will be there, how many windows, how many floors, etc.

Ads code goes here

Each of these houses will have its owner details when they are built and distributed. So in the sense, the houses will look alike but the details of each house like the owner, how many occupants will be there in the house, will vary. Even the interior may vary from one house to another. But the base of creating a house will be the same.

This is the same for roads, the design will be the same but the name, locality, and length will be different. It is similar to create a beautiful application using java.

As a pure object-oriented language, everything in Java should be inside a class.Java classes are the blueprint or master plan just like a master plan of a house.

we can create any number of houses from this master plan. The master plan is called class and the individual houses created from this master plan is called an object.

This is a very interesting Class called Class in java. An instance of the Class Class only represents classes and interfaces in the java applications. There are no member elements and public constructors available for Class Class.

Instances of the Class class are used as run-time descriptions of all Java data types, both reference types, and primitive types. It also supports the Reflection API.

Class objects are constructed automatically by JVM when Classes are loaded into JVM and by the help of the method defineClass()[it stays in ClassLoader Class]

This class represents a Java class or interface, or any Java type. There is one Class object for each class that is loaded into the Java Virtual Machine, there are special Class objects that represent the Java primitive types.

The TYPE constants defined by Boolean, Integer, and the other primitive “wrapper classes” hold these special Class objects. Array types are also represented by Class objects.

There is no constructor for this class. We can obtain a Class object by calling the getClass() method of any instance of the desired class. A Class object may also be referred to by appending .class to the name of a class.

A class can be dynamically loaded bypassing its fully-qualified name (i.e., package name plus class name) to the static Class.forName() method.

This method loads the named class, if it is not already loaded, into the Java interpreter, and returns a Class object for it.

The newInstance() method creates an instance of a given class–this allows you to create instances of dynamically loaded classes for which we cannot use the new keyword.

Classes provide a convenient method for packing together a group of logically related data items and functions that work with them. A class is given a name(called Identifier) to distinguish from other classes of the application.

The class includes physical components called member elements and several methods for operations. It is a datatype where the class creator will define how the new type of objects looks like.

The linking and bonding between data and operations on these data are known as encapsulation. In the context of behavior, in approach, parameters to the behavior and return type should be primitive data type or a class object.

A class is a user-defined data type with a template that defines the properties. Once the class type has been defined, we can create variables of that type using-declaration that is similar to the basic type declarations that are similar to the basic type declaration. These variables are called an instance of the class. (They are objects of that class)

Note This method only works when the target class has a no-argument constructor. See newInstance() in java.lang.reflect.Constructor for a more powerful way to instantiate dynamically loaded classes.

  • getName() returns the name of the class.
  • getSuperclass() returns its superclass.
  • isInterface() tests whether the Class object represents an interface?
  • getInterfaces() returns an array of the interfaces that this class implements.
  • isPrimitive() method tests if the Class object describes a primitive type or not.
  • isArray() tests if the object is an array.

The various other get and is methods return other information about the represented class or interface type  with Java Reflection API ( java.lang.reflect, java.lang.reflect.Field, java.lang.reflect.Method, and java.lang.reflect.Constructor objects.)

Importance of Class file

The Java Class file makes Java code to be transmitted over networks to make Java a platform-independent by supporting network mobility.

It contains the binary form of Java programs. Irrespective of the underlying host operating system the Java Virtual Machine accepts this binary data to execute the Java program.

In traditional binary file is system dependent, hence they may not run any other hosts when created in a separate host.

A Java Compiler translates the instructions of the Java source code files into byte code. Byte code is the machine language for the Java Virtual Machine.

Class Class in Java
Class Class in Java

In addition to processor-specific machine language, other platform-independent attributes of a regular binary executable file are the byte order of integers.

In executable binary files for the Intel X 86 processors the byte order is little ending or lower-order byte first again in executable files for the Power PC chip, the byte order is big ending or higher-order byte first.

Now in Java byte order is a big ending irrespective of the platform that generates the file and irrespective of what platform, on which these files are going to be executed.

Class files are compact in size so that they can be moved easily over the network. By dynamic extension of class files can be downloaded as and when needed. This feature reduces the user wait time.

Below is the structure of Class -Class

public final Class java.lang.Class extends java.lang.Object implements java.io.Serializable{
// No Constructor
// Class Methods
public static native Class forName(String className) throws ClassNotFoundException;
// Public Instance Methods
public native ClassLoader getClassLoader();
public Class[] getClasses();
public native Class getComponentType();
public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Constructor[] getConstructors() throws SecurityException;
public Class[] getDeclaredClasses() throws SecurityException;
public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Constructor[] getDeclaredConstructors() throws SecurityException;
public Field getDeclaredField(String name) throws NoSuchFieldException,SecurityException;
public Field[] getDeclaredFields() throws SecurityException;
public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Method[] getDeclaredMethods() throws SecurityException;
public Class getDeclaringClass();
public Field getField(String name) throws NoSuchFieldException,SecurityException;
public Field[] getFields() throws SecurityException;
public native Class[] getInterfaces();
public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Method[] getMethods() throws SecurityException;
public native int getModifiers();
public native String getName();
public URL getResource(String name);
public InputStream getResourceAsStream(String name);
public native Object[] getSigners();
public native Class getSuperclass();
public native boolean isArray();
public native boolean isAssignableFrom(Class cls);
public native boolean isInstance(Object obj);
public native boolean isInterface();
public native boolean isPrimitive();
public native Object newInstance() throws InstantiationException, IllegalAccessException;
public String toString(); // Overrides Object
} 

The details of the method described are as follows:

public static native Class forName(String className) throws ClassNotFoundException;

public static native Class forName(String className) throws ClassNotFoundException method dynamically loads a class if it has not already been loaded. The method returns a Class object that describes the named class.

The most common use of forName() is for loading classes on the fly when an application wants to use classes it wasn’t built with.

For example, a web browser uses this technique. When a browser needs to load an applet, the browser calls Class.forName() for the applet.

The method loads the class if it has not already been loaded and returns the Class object that encapsulates the class. The browser then creates an instance of the applet by calling the class object’s newInstance() method.

READ  Class Double in Java

When a class is loaded using a ClassLoader object, any classes loaded at the instigation of that class are also loaded using the same ClassLoader object.

This method implements a security policy by trying to find a ClassLoader object to load the named class.

The method searches the stack for the most recently invoked method associated with a class that was loaded using a ClassLoader object. If such a class is found, the ClassLoader object associated with that class is used.

This method returns a Class object that describes the named class.
Parameter
className-Name of a class qualified by the name of its package. If the class is defined inside of another class, all dots(.) that separate the top-level class name from the class to load must be changed to dollar signs ($) for the name to be recognized.

public native ClassLoader getClassLoader();

public native ClassLoader getClassLoader() method returns the ClassLoader object that was used to load this class. If this class was not loaded with a ClassLoader, null is returned.

This method is useful for making sure that a class gets loaded with the same class loader as was used for loading
this Class object.
This method returns the ClassLoader object used to load this class or null if this class was not loaded with a ClassLoader.

public Class[] getClasses();

public Class[] getClasses() method returns an array of Class objects(If this Class object represents a reference type) that lists all of the public classes and interfaces that are members of this class or interface. The list includes public classes and interfaces that are inherited from superclasses and that are defined by this class or interface. If there are no public member classes or interfaces, or if this Class represents a primitive type, the method returns an array of length 0.

This method always returns an array of length 0, no matter how many public member classes this class or interface declares.

This method returns an array of Class objects that contains the public classes and interfaces that are members of this class.

public native Class getComponentType();

public native Class getComponentType() method returns a Class object(If this Class object represents an array type) that describes the component type of the array. If this Class does not represent an array type, the method returns null.

This method returns a  Class object that describes the component type of this class if it is an array type.

public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException;

public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException method returns a Constructor object(If this Class object represents a class) that reflects the specified public constructor of this class.

The constructor is located by searching all of the constructors of the class for a public constructor that has the same formal parameters as specified. If this Class does not represent a class, the method returns null.

This method returns a Constructor object that reflects the specified public constructor of this class.

Parameter
parameterTypes[]-An array of Class objects that describes the parameter types, in declared order, of the constructor.

public Constructor[] getConstructors() throws SecurityException;

public Constructor[] getConstructors() throws SecurityException method returns an array of Constructor objects(If this Class object represents a class,) that reflect the public constructors of this class. If there are no public constructors, or if this Class does not represent a class, the method returns an array of length 0.
This method returns an array of Constructor objects that reflect the public constructors of this class.

public Class[] getDeclaredClasses() throws SecurityException;

public Class[] getDeclaredClasses() throws SecurityException method returns an array of Class objects(If this Class object represents a class) that lists all of the classes and interfaces that are members of this class or interface. The list includes public, protected, default access, and private classes and interfaces that are defined by this class or interface, but it excludes classes and interfaces inherited from superclasses. If there are no such member classes or interfaces, or if this Class represents a primitive type, the method returns an array of length 0.

This method always returns an array of length 0, no matter how many member classes this class or interface declares.
This method returns an array of Class objects that contains all of the declared classes and interfaces that are members of this class.

public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException;

public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException method returns a Constructor object(If this Class object represents a class) that reflects the specified declared constructor of this class.

The constructor is located by searching all of the constructors of the class for the public, protected, default access, or private constructor that has the same formal parameters as specified. If this Class does not represent a class, the method returns null.
This method returns a Constructor object that reflects the specified declared constructor of this class.

Parameter
parameterTypes[]-An array of Class objects that describes the parameter types, in declared order, of the constructor.

public Constructor[] getDeclaredConstructors() throws SecurityException;

public Constructor[] getDeclaredConstructors() throws SecurityException method returns an array of Constructor objects(If this Class object represents a class) that reflect the public, protected, default access, and private constructors of this class. If there are no declared constructors, or if this Class does not represent a class, the method returns an array of length 0.

This method returns an array of Constructor objects that reflect the declared constructors of this class.

public Field getDeclaredField(String name) throws NoSuchFieldException,SecurityException;

public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException method returns a Field object(If this Class object represents a class or interface) that reflects the specified declared field of this class. The field is located by searching all of the fields of the class (but not inherited fields) for the public, protected, default access, or private field that has the specified simple name. If this Class does not represent a class or interface, the method returns null.

This method returns a Field object that reflects the specified declared field of this class.
Parameter
the name-The simple name of the field.

public Field[] getDeclaredFields() throws SecurityException;

public Field[] getDeclaredFields() throws SecurityException method returns an array of Field objects(If this Class object represents a class or interface) that reflect the public, protected, default access, and private fields declared by this class, but excludes inherited fields.

If there are no declared fields, or if this Class does not represent a class or interface, the method returns an array of length 0.

This method does not reflect the implicit length field for array types. The methods of the class Array should be used to manipulate array types.

This method returns an array of Field objects that reflect the declared fields of this class.

public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException;

public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException method returns a Method object(If this Class object represents a class or interface) that reflects the specified declared method of this class.

The method is located by searching all of the methods of the class (but not inherited methods) for the public, protected, default access, or private method that has the specified simple name and the same formal parameters as specified. If this Class does not represent a class or interface, the method returns null.

This method returns an array of Class objects that describes the parameter types, in declared order, of the method.
Parameter
the name-The simple name of the method.

public Method[] getDeclaredMethods() throws SecurityException;

public Method[] getDeclaredMethods() throws SecurityException method returns an array of Method objects(If this Class object represents a class or interface) that reflect the public, protected, default access, and private methods declared by this class but exclude inherited methods. If there are no declared methods, or if this Class does not represent a class or interface, the method returns an array of length 0.

This method returns an array of Method objects that reflect the declared methods of this class.

public Class getDeclaringClass();

public Class getDeclaringClass() method returns a Class object(If this Class object represents a class or interface that is a member of another class or interface) that describes the declaring class or interface. If this class or interface is not a member of another class or interface, or if it represents a primitive type, the method returns null.

READ  Class Math in Java

This method returns a Class object that represents the declaring class if this class is a member of another class.

public Field getField(String name) throws NoSuchFieldException,SecurityException;

public Field getField(String name) throws NoSuchFieldException, SecurityException method returns a Field object(If this Class object represents a class or interface) that reflects the specified public field of this class. The field is located by searching all of the fields of the class, including any inherited fields, for a public field that has the specified simple name. If this Class does not represent a class or interface, the method returns null.

This method returns a Field object that reflects the specified public field of this class.
Parameter
the name-The simple name of the field.

public Field[] getFields() throws SecurityException;

public Field[] getFields() throws SecurityException method returns an array of Field objects(If this Class object represents a class or interface) that reflect the public fields declared by this class and any inherited public fields. If there are no public fields, or if this Class does not represent a class or interface, the method returns an array of length 0.

This method does not reflect the implicit length field for array types. The methods of the class Array should be used to manipulate array types.

This method returns an array of Field objects that reflect the public fields of this class.

public native Class[] getInterfaces();

public native Class[] getInterfaces() method returns an array that refers to all of the interfaces that the class implements (If the Class object represents a class). The order of the interfaces referred to in the array is the same as the order in the class declaration’s implements clause. If the class does not implement any interfaces, the length of the returned array is 0.

If the object represents an interface, this method returns an array that refers to all of the interfaces that this interface extends. The interfaces occur in the order they appear in the interface declaration’s extends clause. If the interface does not extend any interfaces, the length of the returned array is 0.

If the object represents a primitive or array type, the method returns an array of length 0.

This method returns an array of the interfaces implemented by this class or extended by this interface.

public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException;

public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException method returns a Method object(If this Class object represents a class or interface) that reflects the specified public method of this class.

The method is located by searching all of the methods of the class, including any inherited methods, for a public method that has the specified simple name and the same formal parameters as specified. If this Class does not represent a class or interface, the method returns null.

This method returns a Method object that reflects the specified public method of this class.
Parameter
the name-The simple name of the method.
a parameterTypes-An array of Class objects that describes the parameter types, in declared order, of the method

public Method[] getMethods() throws SecurityException;

public Method[] getMethods() throws SecurityException method returns an array of Method objects(If this Class object represents a class or interface) that reflect the public methods declared by this class and any inherited public methods. If there are no public methods or if this Class doesn’t represent a class or interface, the method returns an array of length 0.

This method returns an array of Method objects that reflect the public methods of this class.

public native int getModifiers();

public native int getModifiers() method returns an integer value that represents the modifiers used to declare the class or interface If this Class object represents a class or interface. The Modifier class should be used to decode the returned value.

This method returns an integer that represents the modifier keywords used to declare this class.

public native String getName();

public native String getName() method returns the fully qualified name of the type represented by this Class object.

If the object represents the class of an array, the method returns a String that contains as many left square brackets as there are dimensions in the array, followed by a code that indicates the type of element contained in the base array. Consider the following:

(new int [5][6][2]).getClass().getName()

This code returns “[[[I”. The codes used to indicate the element type are as follows:

CodeType
[array
Bbyte
Cchar
ddouble
Ffloat
Iint
Jlong
Lfully_qualified_class_name class or interface
Sshort
Zboolean

This method returns the fully qualified name of this class or interface.

public URL getResource(String name);

public URL getResource(String name) method finds a resource with the given name for this Class object and returns a URL object that is connected to the resource.

The rules for searching for a resource associated with a class are implemented by the ClassLoader for the class; this method simply calls the getResource() method of the ClassLoader.

If this class does not have a ClassLoader (i.e., it is a system class), the method calls the ClassLoader.getSystemResource() method.

This method returns a URL object that is connected to the specified resource or null if the resource cannot be found.
Parameter
name-A A resource name.

public InputStream getResourceAsStream(String name);

public InputStream getResourceAsStream(String name) method finds a resource with the given name for this Class object and returns an InputStream object that is connected to the resource. The rules for searching for a resource associated with a class are implemented by the ClassLoader for the class; this method simply calls the getResourceAsStream() method of the ClassLoader. If this class does not have a ClassLoader (i.e., it is a system class), the method calls the ClassLoader.getSystemResourceAsStream() method.

This method returns an InputStream object that is connected to the specified resource or null if the resource cannot be found.
Parameter
name-A A resource name.

public native Object[] getSigners();

public native Object[] designersgetSigners() method returns an array of objects that represents the digital signatures for this class.

This method returns an array of Objects that represents the signers of this class.

public native Class getSuperclass();

public native Class getSuperclass() method returns the Class object(If the Class object represents a class other than Object) that represents its superclass. If the object represents an interface, the Object class, or a primitive type, the method returns null.

This method returns the superclass of this class or null if there is no superclass.

public native boolean isArray();

public native boolean isArray() method checks if the object is an array.

This method returns true if this object describes an array type; otherwise false

public native boolean isAssignableFrom(Class cls);

public native boolean is assignableFrom(Class cls) method determines whether or not the type represented by class is assignable to the type of this class. If this class represents a class, this class must be the same as a class or a superclass of the class. If this class represents an interface, this class must be the same as cls or a super interface of a class. If this class represents a  primitive type, this class must be the same as cls.

Class Class in Java
Class Class in Java

This method returns true if the type represented by class is assignable to the type of this class: otherwise false.

Parameter
cls-A Class object to be tested.

public native boolean isInstance(Object obj);

public native boolean is the Instance(Object obj) method determines whether or not the object represented by obj can be cast to the type of this class object without causing a ClassCastException. This method is the dynamic equivalent of the instanceof operator.

This method returns true if obj can be cast to the reference type specified by this class; otherwise false.
Parameter
obj-An Object to be tested.

public native boolean isInterface();

public native boolean isInterface() method checks if the given object is an interface.
This method returns true if this object describes an interface; otherwise false

public native boolean isPrimitive();

public native boolean isPrimitive() method checks if the given object is a primitive type.
This method returns true if this object describes a primitive type; otherwise false.

public native Object newInstance() throws InstantiationException, IllegalAccessException;

public native Object newInstance() throws InstantiationException, IllegalAccessException method creates a new instance of this class by performing these steps:

  • It creates a new object of the class represented by the Class object.
  • It calls the constructor for the class that takes no arguments.
  • It returns a reference to the initialized object.
READ  Variables in Java

The newInstance() method is useful for creating an instance of a class that has been dynamically loaded using the forName() method.

The reference returned by this method is usually cast to the type of object that is instantiated.

The newInstance() method can throw objects that are not instances of the classes it is declared to throw. If the constructor invoked by newInstance() throws an exception, the exception is thrown by newInstance() regardless of the class of the object.

This method returns a reference to a new instance of this class.
Parameter

public String toString(); // Overrides Object

public String toString(); method returns a string representation of the Class object.
This method returns a String that contains the name of the class with either ‘class’ or ‘interface’ prepended as appropriate.
Apart from these Compiler class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

The basic structure of the class is :

class className[extends superclass][implements someOtherclass]
{
[field declaration]
[methods declaration]
} 

Filed declaration

Data encapsulated in a class by placing data fields inside the body of the class definition. These variables are called instance variables as they are created whenever an object of the class is instantiated. The declaration is the same as local variables.


class Rectangle{
int length;
int width;
}
 

Once they are declared, there is no space allocated to them.

Design of a class

While designing qualifier “-” signifies private and “+” signifies public.
like
-firstName=>String but private
+setFirstName(firstName)=> String but public

Collectively the variables and methods of a class are often referred to as members or fields of a class.
The transformation from a conceptual class diagram to class code is depicted here:

Class DiagramClass Code
MyClass———————–>public class MyClass
-firstName:String———————–>private firstName
+setFirstName(firstName:String)———————–>public  void setFirstName(String firstName)
{
this.firstName=firstName;
}
+getFirstName():String———————–>public String getFirstName()
{
return firstName;
}
so on….

the constructor is omitted here, but in reality, we will have a constructor for a class.
Below is the general formula for a class:


<<Access specifier>> class className [extends SuperClass Name] implements interface1,interface2,....interfaceN{
//member elements mostly private
[Deceleration of member elements]
//methods mostly public
[Deceleration of methods]
}

Method declaration:

A class with only data fields has no life as there are no methods that operate on that data. The objects created by such a class can not respond to any message.

So it is necessary to design methods. Methods are declared inside the body of the class but immediately after the declaration of the instance variables.


[access modifier][specifier][return type]methodname(parameterList)
{
//method body
}
 

The four basic parts of the valid method are as follows:

  1. The name of the method(method name)
  2. The return type of the method
  3. the list of parameters
  4. the body of the method.

We can pass an object and some primitive data type to a method in the form of an argument and can return an object or primitive data type.
like setValue(int xyz);
anything inside the first bracket is considered as payload to the function. It is a mechanism by which we either pass the value of data or reference to the object. Certainly, a class has a function with an empty payload. Like
setValue();


public class MyClass{
private String firstName();
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getFirstName(){
return firstName;
}
}
 

In the above statement, MyClass is an example of the structure of the class having one variable called firstName.If the constructor of the class MyClass() is called, the variable is set to the default value. In case, the constructor sets the value, it will be overridden.
Therefore every member variable will have a setter and a getter method so that the objects created from the class pass and change the values.
like setXXX(datatype value);//value passed
getXXX(){return datatype;}//method with no payload

The return type could be inbuilt data types or a class object. A method may not have any valid return value, in such cases, the return type will be void. The argument list is enclosed with parentheses. The list contains variables in a list separated by a comma. A method may also take no input in such case the argument list is empty parentheses.”()”.
like:
getValue();

Most of the time, when we use classes, we will have many methods and variables within the class. Instance variables and methods in classes are accessible by all methods in the class but a method can not access variables declared in another method.


class abc{
int x;
//legal
void method1()
{
x=10;
int y=x;
}
void method2()
{
int z;
x=5;
z=10;
//illegal as y is part of method1
y=1;
}

The data member of a class

The data member is the thing we want to communicate via its handle. It can be of any type(primitive) which is not handled. It can be a handle to an object(it must be initialized to connect it to an actual object using new in the constructor). If it is a primitive data type, we can initialize it directly at the point of definition in the class.

Guidelines for class creation:

    • By convention, a class name starts with a capital letter. like PrintStream
    • class is a keyword and declares that a new class definition as follows.
    • the class definition in Java starts with an opening brace (“{“)  and ends with a closing brace.(“}”).
    • the class contains two types of elements-Data members called fields and member functions called methods.
  • Methods in java can be created as a part of the Class. A method can only be called for an object. Except static methods can be called for the class without an object.

Java being a single-rooted language, all classes are a child of Object class.


public class MyClass{..}
//is equivalent to 
public class MyClass extends java.lang.Object{...}
 

Semantically java.lang.Object class is an abstract class but syntactically it is not declared as abstract. As it is a concrete class we can create an object of the Object class.
so


Object o=new Object() //is legal but not sure where to use its methods
a.methods();
 

Methods, arguments, and return value:

Initially, a function is termed as a named subroutine. The method is described as “a way to do something”.Another way, methods in java determine the messages, an object can receive. the structure of the method is as follows:


returnType method Name(argumentList)
{
//method body
//return statement
}
 

returnType is the type of value that pops out of the method after we call it. methodName is the meaningful name we can imagine and identifies the method. The argument list is the list of the type of arguments and names for the information we want to pass to the method. If we try to call the wrong method for an object, we will get a compile-time error. The generic syntax for calling a method:
objectName(handle).(dot).methodName() with argument or without any argument.
If the class on whose handle we call the method (), has the method written, we will be able to perform the method call.
The return value must compatible with the return value defined in the method signature.
The call of the method is commonly referred to as sending the message to the object.

How to get a Class object for a particular data type?

Below are the ways by which we can get a Class object for a particular datatype.

  • By calling the object’s getClass()method. Every class inherits this method from the Object class.
  • Using the new class literal syntax. A class literal is simply the name of a type (a class name or a primitive type name) followed by a period and the class keyword.
Class str = String.class;
Class intVal = int.class;
Class vectorVal = java.util.Vector.class;
  • Using the forName() class method of Class.
Class vectorVal = Class.forName("java.util.Vector");
  • Using newInstance() method of a Class object, if the class has a constructor that takes no arguments.

Share and Enjoy !

1 thought on “Class Class in Java: Know All-inclusive Just In 20 Minutes”

  1. Pingback: Class NoClassDefFoundError in Java: Comprehensive Guide In Just 5 Minutes - TechTravelHub

Leave a Comment

Your email address will not be published. Required fields are marked *