Found 9326 Articles for Object Oriented Programming

Get the list of all declared fields in Java

Smita Kapse
Updated on 25-Jun-2020 12:21:02

7K+ Views

The list of all declared fields can be obtained using the java.lang.Class.getDeclaredFields() method as it returns an array of field objects. These field objects include the objects with the public, private, protected and default access but not the inherited fields.Also, the getDeclaredFields() method returns a zero length array if the class or interface has no declared fields or if a primitive type, array class or void is represented in the Class object.A program that demonstrates this is given as follows −Example Live Demoimport java.lang.reflect.*; public class Demo {    public static void main(String[] argv) throws Exception {       Class ... Read More

Get all declared fields from a class in Java

Krantik Chavan
Updated on 25-Jun-2020 12:22:18

3K+ Views

An array of field objects is returned by the method java.lang.Class.getDeclaredFields(). These field objects include the objects with the public, private, protected and default access but not the inherited fields.Also, the getDeclaredFields() method returns a zero length array if the class or interface has no declared fields or if a primitive type, array class or void is represented in the Class object.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.reflect.*; public class Demo {    int i;    char c;    public Demo(int i, char c) {       this.i = i;     ... Read More

How to call Private Constructor in Java

Anvi Jain
Updated on 25-Jun-2020 12:23:27

913 Views

The method java.lang.Class.getDeclaredConstructor() can be used to obtain the constructor object for the private constructor of the class. The parameter for this method is a Class object array that contains the formal parameter types of the constructor.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.reflect.*; public class Demo {    String str;    Double d;    public Demo(String str, Double d) {       this.str = str;       this.d = d;    }    public static void main(String[] args) {       try {          Demo obj = ... Read More

Load class with forName() method in Java

Nancy Den
Updated on 25-Jun-2020 12:24:16

2K+ Views

The class object associated with the class with the given string name can be returned with the method java.lang.Class.forName(String name, boolean initialize, ClassLoader loader), using the class loader that is used to load the class.The parameters in the forName() method are name, initialize and loader. If the value of the parameter loader is null, then the class is loaded using the bootstrap class loader. Also, if the initialize parameter is true, then only the class is initialized if it has not been initialized earlier.A program that loads the class using the forName() method is given as follows −Example Live Demoimport java.lang.*; ... Read More

Prove that the interface for a primitive type is an empty array in Java

Anvi Jain
Updated on 25-Jun-2020 12:25:30

88 Views

The getInterfaces() method is used to prove that the interface for a primitive type is an empty array. A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.util.*; public class Demo {    public static void main(String[] args) {       Class c = int.class;       Class[] interfaces = c.getInterfaces();       System.out.println("The Interface is: " + Arrays.asList(interfaces));    } }OutputThe Interface is: []Now let us understand the above program.The int.class is a reference to the Class object for the primitive type int. The interface for this is determined using the getInterfaces() ... Read More

Compare two char arrays in a single line in Java

Smita Kapse
Updated on 25-Jun-2020 12:26:39

3K+ Views

Two char arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two char arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new char[] { 'a', 'b', 'c' }, new char[] { 'a', 'b', 'c' });       System.out.println("The two char arrays are equal? ... Read More

Check whether String is an interface or class in Java

Nancy Den
Updated on 25-Jun-2020 12:27:18

462 Views

The method java.lang.Class.isInterface() is used to find if String is an interface or class in Java. This method returns true if String is an interface, otherwise it returns false.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.*; public class Demo {    public static void main(String[] args) {       Class c = String.class;       boolean interfaceFlag = c.isInterface();       System.out.println("This is an Interface? " + interfaceFlag);    } }OutputThis is an Interface? falseNow let us understand the above program.The isInterface() method is used to find if String is an ... Read More

List the Interfaces that an Interface Extends in Java

Krantik Chavan
Updated on 25-Jun-2020 12:27:52

288 Views

The interfaces that are implemented by an interface that is represented by an object can be determined using the java.lang.Class.getInterfaces() method. This method returns an array of all the interfaces that are implemented by the interface.A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.*; import java.util.*; public class Demo {    public static void main(String[] args) {       listInterfaces(java.util.List.class);    }    public static void listInterfaces(Class c) {       System.out.println("The interface is: " + c.getName());       Class[] interfaces = c.getInterfaces();       System.out.println("The Interfaces are: " + Arrays.asList(interfaces)); ... Read More

Demonstrate getting the immediate superclass information in Java

Anvi Jain
Updated on 30-Jul-2019 22:30:24

108 Views

The immediate superclass information of any entity such as an object, class, primitive type, interface etc. can be obtained using the method java.lang.Class.getSuperclass().A program that demonstrates this is given as follows −Example Live Demopackage Test; import java.lang.*; class Class1{ } class Class2 extends Class1{ } public class Demo { public static void main(String args[]) { Class1 obj1 = new Class1(); Class2 obj2 = new Class2(); Class c; c = obj2.getClass(); ... Read More

Get the declared method by name and parameter type in Java

Smita Kapse
Updated on 25-Jun-2020 12:29:02

708 Views

The declared method can be obtained by name and parameter type by using the java.lang.Class.getDeclaredMethod() method. This method takes two parameters i.e. the name of the method and the parameter array of the method.The getDeclaredMethod() method returns a Method object for the method of the class that matches the name of the method and the parameter array that are the parameters.A program that gets the declared method by name and parameter type using the getDeclaredMethod() method is given as follows −Example Live Demopackage Test; import java.lang.reflect.*; public class Demo {    public String str;    private Integer func1() {     ... Read More

Advertisements