Java.lang.Class class in Java | Set 1
More methods:
1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Modifier class in java.lang.Reflect package.
Syntax :
public int getModifiers()
Parameters :
NA
Returns :
the int representing the modifiers for this class
Java
// Java program to demonstrate getModifiers() method
import java.lang.reflect.Modifier;
public abstract class Test
{
public static void main(String[] args)
{
// returns the Class object associated with Test class
Class c = Test.class;
// returns the Modifiers of the class Test
// getModifiers method
int i = c.getModifiers();
System.out.println(i);
System.out.print("Modifiers of " + c.getName() + " class are : ");
// getting decoded i using toString() method
// of Modifier class
System.out.println(Modifier.toString(i));
}
}
Output:
1025
Modifiers of Test class are : public abstract
2. T[] getEnumConstants() : This method returns the elements of this enum class. It returns null if this Class object does not represent an enum type.
Syntax :
public T[] getEnumConstants()
Parameters :
NA
Returns :
an array containing the values comprising the enum class represented by this Class object
in the order they're declared,
or null if this Class object does not represent an enum type
Java
// Java program to demonstrate getEnumConstants() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
public static void main(String[] args)
{
// returns the Class object associated with Color(an enum class)
Class c1 = Color.class;
// returns the Class object associated with Test class
Class c2 = Test.class;
// returns the elements of Color enum class in an array
// getEnumConstants method
Object[] obj1 = c1.getEnumConstants();
System.out.println("Enum constants of " + c1.getName() + " class are :");
// iterating through enum constants
for (Object object : obj1)
{
System.out.println(object);
}
// returns null as Test Class object does not represent an enum type
Object[] obj2 = c2.getEnumConstants();
System.out.println("Test class does not contain any Enum constant.");
System.out.println(obj2);
}
}
Output:
Enum constants of Color class are :
RED
GREEN
BLUE
Test class does not contain any Enum constant.
null
3. String getCanonicalName() : This method returns the canonical name of the underlying class as defined by the Java Language Specification.
It returns null if the underlying class does not have a canonical name (i.e., if it is a local or anonymous class or an array whose component type does not have a canonical name).
Syntax :
public String getCanonicalName()
Parameters :
NA
Returns :
the Canonical name of the underlying class, if it exists
null, otherwise
Java
// Java program to demonstrate getCanonicalName() method
public class Test
{
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c1 = Class.forName("java.lang.String");
System.out.print("Canonical name of class represented by c1 : ");
// returns the Canonical name of the class
// getCanonicalName method
System.out.println(c1.getCanonicalName());
}
}
Output:
Canonical name of class represented by c1 : java.lang.String
4. boolean desiredAssertionStatus() : This method returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
Syntax :
public boolean desiredAssertionStatus()
Parameters :
NA
Returns :
the desired assertion status of the specified class.
Java
// Java program to demonstrate desiredAssertionStatus() method
public class Test
{
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c1 = Class.forName("java.lang.String");
// checking for assertion status of String class
System.out.print("desired assertion status of " + c1.getName() + "class: ");
// desiredAssertionStatus() method
System.out.println(c1.desiredAssertionStatus());
}
}
Output:
desired assertion status of java.lang.Stringclass : false
5. Class<?> getComponentType() : This method returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.
Syntax :
public Class<?> getComponentType()
Parameters :
NA
Returns :
the Class representing the component type of this class if this class is an array
Java
// Java program to demonstrate getComponentType() method
public class Test
{
public static void main(String[] args)
{
int a[] = new int[2];
// returns the Class object for array class
Class c = a.getClass();
System.out.print("Component type of class represented by c : ");
// getComponentType() method
System.out.println(c.getComponentType());
}
}
Output:
Component type of class represented by c : int
6. Class<?>[] getDeclaredClasses() : Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
This method includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. This method returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type, an array class, or void.
Syntax :
public Class<?>[] getDeclaredClasses()
Parameters :
NA
Returns :
the array of Class objects representing all the declared members of this class
Throws :
SecurityException - If a security manager, s, is present
Java
// Java program to demonstrate getDeclaredClasses() method
public class Test
{
// base interface
interface A
{
// methods and constant declarations
}
// derived class
class B implements A
{
// methods implementations that were declared in A
}
public static void main(String[] args)
{
// returns the Class object associated with Test class
Class myClass = Test.class;
// getDeclaredClasses on myClass
// it returns array of classes and interface declare in Test class
Class c[] = myClass.getDeclaredClasses();
System.out.println("Declared classes and interfaces present in " +
myClass.getName() + " class : ");
// iterating through classes and interfaces declared in Test class
for (Class class1 : c)
{
System.out.println(class1);
}
}
}
Output:
Declared classes and interfaces present in Test class :
interface Test$A
class Test$B
7. Field getDeclaredField(String fieldName) : This method returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
The name parameter is a String that specifies the simple name of the desired field. Note that this method will not reflect the length field of an array class.
Syntax :
public Field getDeclaredField(String fieldName)
throws NoSuchFieldException,SecurityException
Parameters :
fieldName - the field name
Returns :
the Field object for the specified field in this class
Throws :
NoSuchFieldException - if a field with the specified name is not found.
NullPointerException - if fieldName is null
SecurityException - If a security manager, s, is present.
Java
// Java program to demonstrate getDeclaredField() method
import java.lang.reflect.Field;
public class Test
{
// any declared field
int i;
public static void main(String[] args)
throws NoSuchFieldException, SecurityException
{
// returns the Class object associated with Test class
Class myClass = Test.class;
// getDeclaredField on myClass
Field f = myClass.getDeclaredField("i");
System.out.println("Declared field present in " +
myClass.getName() +
" class specified by \"i\" : ");
System.out.println(f);
}
}
Output:
Declared field present in Test class specified by "i" :
int Test.i
8. Field[] getDeclaredFields() : This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.
Syntax :
public Field[] getDeclaredFields() throws SecurityException
Parameters :
NA
Returns :
the array of Field objects representing all the declared fields of this class
Throws :
SecurityException - If a security manager, s, is present.
Java
// Java program to demonstrate getDeclaredFields() method
import java.lang.reflect.Field;
public class Test
{
// some declared fields
int i;
String str;
boolean b;
public static void main(String[] args)
{
// returns the Class object associated with Test class
Class myClass = Test.class;
// getDeclaredFields on myClass
Field f[] = myClass.getDeclaredFields();
System.out.println("Declared fields present in " +
myClass.getName() + " class are : ");
// iterating through declared fields of Test class
for (Field field : f)
{
System.out.println(field);
}
}
}
Output:
Declared fields present in Test class are :
int Test.i
java.lang.String Test.str
boolean Test.b
9. Method getDeclaredMethod(String methodName,Class... parameterTypes) : This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
Syntax :
public Method getDeclaredMethod(String methodName,Class... parameterTypes)
throws NoSuchFieldException,SecurityException
Parameters :
methodName - the method name
parameterTypes - the list of parameters
Returns :
the Method object for the method of this class matching the specified name and parameters
Throws :
NoSuchMethodException - if a method with the specified name is not found.
NullPointerException - if methodName is null
SecurityException - If a security manager, s, is present.
Java
// Java program to demonstrate getDeclaredMethod() method
import java.lang.reflect.Method;
public class Test
{
// any declared method
// with a String argument
public void m1(String str)
{
System.out.println(str);
}
public static void main(String[] args)
throws NoSuchMethodException, SecurityException, ClassNotFoundException
{
// returns the Class object associated with Test class
Class myClass = Test.class;
// returns the Class object for the class
// with the specified name
Class c = Class.forName("java.lang.String");
// getDeclaredMethod on myClass
Method m = myClass.getDeclaredMethod("m1",c);
System.out.println("Declared method present in " +
myClass.getName() +
" class specified by argument : " + c.getName());
System.out.println(m);
}
}
Output:
Declared method present in Test class specified by argument : java.lang.String
public void Test.m1(java.lang.String)
10. Method[] getDeclaredMethods() : This method returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private methods, but excludes inherited methods.
This method returns an array of length 0 if the class or interface declares no methods, or if this Class object represents a primitive type, an array class, or void.
Syntax :
public Method[] getDeclaredMethods() throws SecurityException
Parameters :
NA
Returns :
the array of Method objects representing all the declared methods of this class
Throws :
SecurityException - If a security manager, s, is present.
Java
// Java program to demonstrate getDeclaredMethods() method
import java.lang.reflect.Method;
public class Test
{
// some declared Methods
public void m1()
{
System.out.println("Inside m1 method");
}
static void m2()
{
System.out.println("Inside m2 method");
}
// main method
public static void main(String[] args)
{
// returns the Class object associated with Test class
Class myClass = Test.class;
// getDeclaredMethods on myClass
Method m[] = myClass.getDeclaredMethods();
System.out.println("Declared methods present in " +
myClass.getName() + " class are : ");
// iterating through declared Methods of Test class
for (Method Method : m)
{
System.out.println(Method);
}
}
}
Output:
Declared methods present in Test class are :
public static void Test.main(java.lang.String[])
public void Test.m1()
static void Test.m2()
11. Constructor<?> getDeclaredConstructor(Class<?>... parameterTypes) : This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
Syntax :
public Constructor<?> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException,SecurityException
Parameters :
parameterTypes - the list of parameters
Returns :
The Constructor object for the constructor with the specified parameter list
Throws :
NoSuchMethodException - if a Constructor with the specified parameterTypes is not found.
SecurityException - If a security manager, s, is present.
Java
// Java program to demonstrate getDeclaredConstructor() Constructor
import java.lang.reflect.Constructor;
public class Test
{
public static void main(String[] args)
throws NoSuchMethodException, SecurityException, ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c1 = Class.forName("java.lang.Integer");
Class c2 = Class.forName("java.lang.String");
// getDeclaredConstructor on myClass
Constructor con = c1.getDeclaredConstructor(c2);
System.out.println("Declared Constructor present in " + c1.getName() +
" class specified by argument : " + c2.getName());
System.out.println(con);
}
}
Output:
Declared Constructor present in java.lang.Integer class specified by argument :
java.lang.String public java.lang.Integer(java.lang.String)
throws java.lang.NumberFormatException
12. Constructor<?>[] getDeclaredConstructors() : This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. These are public, protected, default (package) access, and private constructors.
This method returns an array of length 0 if this Class object represents an interface, a primitive type, an array class, or void.
Syntax :
public Constructor<?>[] getDeclaredConstructors() throws SecurityException
Parameters :
NA
Returns :
the array of Constructor objects representing all the declared constructors of this class
Throws :
SecurityException - If a security manager, s, is present.
Java
// Java program to demonstrate getDeclaredConstructors() Constructor
import java.lang.reflect.Constructor;
public class Test
{
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c = Class.forName("java.lang.String");
// getDeclaredConstructors on myClass
Constructor con[] = c.getDeclaredConstructors();
System.out.println("Declared Constructors present in " +
c.getName() + " class are : ");
// iterating through all constructors
for (Constructor constructor : con)
{
System.out.println(constructor);
}
}
}
Output:
Declared Constructors present in java.lang.String class are :
public java.lang.String(byte[],int,int)
public java.lang.String(byte[],java.nio.charset.Charset)
public java.lang.String(byte[],java.lang.String) throws
java.io.UnsupportedEncodingException
public java.lang.String(byte[],int,int,java.nio.charset.Charset)
public java.lang.String(byte[],int,int,java.lang.String)
throws java.io.UnsupportedEncodingException
java.lang.String(char[],boolean)
public java.lang.String(java.lang.StringBuilder)
public java.lang.String(java.lang.StringBuffer)
public java.lang.String(byte[])
public java.lang.String(int[],int,int)
public java.lang.String()
public java.lang.String(char[])
public java.lang.String(java.lang.String)
public java.lang.String(char[],int,int)
public java.lang.String(byte[],int)
public java.lang.String(byte[],int,int,int)
13. Class<?> getDeclaringClass() : If the class or interface represented by this Class object is a member of another class, then this method returns the Class object representing the class in which it was declared.
This method returns null if this class or interface is not a member of any other class. If this Class object represents an array class, a primitive type, or void,then this method returns null.
Syntax :
public Class<?> getDeclaringClass()
Parameters :
NA
Returns :
the declaring class of this class
Java
// Java program to demonstrate
// getDeclaringClass() Class
import java.lang.reflect.Method;
public class Test
{
// any method
public void m1()
{
System.out.println("Inside m1 method");
}
public static void main(String[] args)
{
// returns A class object
Class c1 = Test.class;
// getting all methods of Test class
// Note that methods from Object class
// are also inherited
Method m[] = c1.getMethods();
for (Method method : m)
{
// getDeclaringClass method
// it return declared class of this method
System.out.println(method.getName() + " is present in "
+ method.getDeclaringClass());
}
}
}
Output:
main is present in class Test
m1 is present in class Test
wait is present in class java.lang.Object
wait is present in class java.lang.Object
wait is present in class java.lang.Object
equals is present in class java.lang.Object
toString is present in class java.lang.Object
hashCode is present in class java.lang.Object
getClass is present in class java.lang.Object
notify is present in class java.lang.Object
notifyAll is present in class java.lang.Object
14. Class<?> getEnclosingClass() : This method returns the immediately enclosing class of the underlying class. If the underlying class is a top level class this method returns null.
Syntax :
public Class<?> getEnclosingClass()
Parameters :
NA
Returns :
the immediately enclosing class of the underlying class
Java
// Java program to demonstrate getEnclosingClass() Class
public class Test
{
// any inner class of Test class
class A{}
public static void main(String[] args)
{
// returns A class object
Class c1 = Test.A.class;
// getEnclosingClass method
// it returns the class object where A class present
Class c2 = c1.getEnclosingClass();
System.out.println("The class " + c1.getName() + " is present in class : ");
System.out.println(c2);
}
}
Output:
The class Test$A is present in class :
class Test
15. Method getEnclosingMethod() : If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
Syntax :
public Class<?> getEnclosingMethod()
Parameters :
NA
Returns :
the immediately enclosing method of the underlying class,
if that class is a local or anonymous class;
otherwise null
Java
// Java program to demonstrate getEnclosingMethod() method
import java.lang.reflect.Method;
public class Test
{
// any method
public static Class m1()
{
// any local class in m1
class A{};
// returning class A
return A.class;
}
// main method
public static void main(String[] args)
throws ClassNotFoundException
{
// returns A Class object
Class c = Test.m1();
// getEnclosingMethod method
Method m = c.getEnclosingMethod();
System.out.println("The class " + c.getName() + " is present in method : ");
System.out.println(m);
}
}
Output:
The class Test$1A is present in method :
public static java.lang.Class Test.m1()
16. Constructor getEnclosingConstructor() : If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class. Returns null otherwise.
Syntax :
public Constructor<?> getEnclosingConstructor()
Parameters :
NA
Returns :
the immediately enclosing constructor of the underlying class,
if that class is a local or anonymous class;
otherwise null.
Java
// Java program to demonstrate
// getEnclosingConstructor() Constructor
import java.lang.reflect.Constructor;
public class Test
{
Class c;
// default(any) constructor
public Test()
{
// any local class
class A{};
// returns A class object
c = A.class;
}
public static void main(String[] args)
{
// creating Test class object
Test t = new Test();
// getEnclosingConstructor method
Constructor con = t.c.getEnclosingConstructor();
System.out.println("The class " + t.c.getName()
+ " is present in constructor : ");
System.out.println(con);
}
}
Output:
The class Test$1A is present in Constructor :
public Test()
17. ProtectionDomain getProtectionDomain() : Returns the ProtectionDomain of this class. If there is a security manager installed, this method first calls the security manager's checkPermission method with a RuntimePermission("getProtectionDomain") permission to ensure it's ok to get the ProtectionDomain.
Syntax :
public ProtectionDomain getProtectionDomain()
Parameters :
NA
Returns :
the ProtectionDomain of this class
Throws :
SecurityException - if a security manager s exists
and its checkPermission method doesn't allow getting the ProtectionDomain.
Java
// Java program to demonstrate getProtectionDomain() method
public class Test
{
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for the class
// with the specified name
Class c1 = Class.forName("java.lang.String");
// checking for assertion status of String class
System.out.println("protection domain of " + c1.getName() + " class : ");
// getProtectionDomain() method
// it will print null as String class is loaded by
// BootStrap class loader
System.out.println(c1.getProtectionDomain());
}
}
Output:
protection domain of java.lang.String class :
ProtectionDomain null
null
java.security.Permissions@7852e922 (
("java.security.AllPermission" "" "")
)
18. boolean isAnnotationPresent(Class<?extends Annotation> annotationClass)() : This method returns true if an annotation for the specified type is present on this element, else false. This method is designed primarily for convenient access to marker annotations.
Specified by:
isAnnotationPresent in interface AnnotatedElement
Syntax :
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
Parameters :
annotationClass - the Class object corresponding to the annotation type
Returns :
true if an annotation for the specified annotation type is present on this element
false, otherwise
Throws:
NullPointerException - if the given annotation class is null
19. boolean isSynthetic() : This method determines if this class is a synthetic class or not.
Syntax :
public boolean isSynthetic()
Parameters :
NA
Returns :
return true if and only if this class is a synthetic class.
20. URL getResource(String name) : Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
Syntax :
public URL getResource(String name)
Parameters :
name - name of the desired resource
Returns :
A URL object or null if no resource with this name is found
21. InputStream getResourceAsStream(String name) : Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
Syntax :
public InputStream getResourceAsStream(String name)
Parameters :
name - name of the desired resource
Returns :
A InputStream object or null if no resource with this name is found
Throws:
NullPointerException - If name is null
22. Object[] getSigners() : Gets the signers of this class.
Syntax :
public Object[] getSigners()
Parameters :
NA
Returns :
the signers of this class, or null if there are no signers.
In particular,it returns null if this object represents a primitive type or void.
23. Annotation[] getAnnotations() : This method returns all annotations present on this element. It returns an array of length zero if this element has no annotations.
The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
Specified by:
getAnnotations() in interface AnnotatedElement
Syntax :
public Annotation[] getAnnotations()
Parameters :
NA
Returns :
all annotations present on this element
24. <A extends Annotation> A getAnnotation(Class<A> annotationClass) : This method returns this element's annotation for the specified type if such an annotation is present, else null.
Specified by:
getAnnotations() in interface AnnotatedElement
Syntax :
public Annotation[] getAnnotations()
Parameters :
annotationClass - the Class object corresponding to the annotation type
Returns :
return element's annotation for the specified annotation type if present on this element
else null
Throws:
NullPointerException - if the given annotation class is null
25. Annotation[] getDeclaredAnnotations() : Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. It returns an array of length zero if no annotations are directly present on this element.
The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
Specified by:
getDeclaredAnnotations in interface AnnotatedElement
Syntax :
public Annotation[] getDeclaredAnnotations()
Parameters :
NA
Returns :
All annotations directly present on this element
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. Known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Syntax and s
10 min read
Basics
Introduction to JavaJava is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Java Programming BasicsJava is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming console
4 min read
Java MethodsJava Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
7 min read
Access Modifiers in JavaIn Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself, can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. In this article
6 min read
Arrays in JavaIn Java, an array is an important linear data structure that allows us to store multiple values of the same type. Arrays in Java are objects, like all other objects in Java, arrays implicitly inherit from the java.lang.Object class. This allows you to invoke methods defined in Object (such as toStri
9 min read
Java StringsIn Java, a String is the type of object that can store a sequence of characters enclosed by double quotes and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowin
8 min read
Regular Expressions in JavaIn Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressi
7 min read
OOPs & Interfaces
Classes and Objects in JavaIn Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. A class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects.An
10 min read
Java ConstructorsIn Java, constructors play an important role in object creation. A constructor is a special block of code that is called when an object is created. Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes. This process happens automaticall
10 min read
Java OOP(Object Oriented Programming) ConceptsBefore Object-Oriented Programming (OOPs), most programs used a procedural approach, where the focus was on writing step-by-step functions. This made it harder to manage and reuse code in large applications.To overcome these limitations, Object-Oriented Programming was introduced. Java is built arou
10 min read
Java PackagesPackages in Java are a mechanism that encapsulates a group of classes, sub-packages and interfaces. Packages are used for: Prevent naming conflicts by allowing classes with the same name to exist in different packages, like college.staff.cse.Employee and college.staff.ee.Employee.They make it easier
8 min read
Java InterfaceAn Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
11 min read
Collections
Exception Handling
Java Exception HandlingException handling in Java is an effective mechanism for managing runtime errors to ensure the application's regular flow is maintained. Some Common examples of exceptions include ClassNotFoundException, IOException, SQLException, RemoteException, etc. By handling these exceptions, Java enables deve
8 min read
Java Try Catch BlockA try-catch block in Java is a mechanism to handle exceptions. This make sure that the application continues to run even if an error occurs. The code inside the try block is executed, and if any exception occurs, it is then caught by the catch block.Example: Here, we are going to handle the Arithmet
4 min read
Java final, finally and finalizeIn Java, the keywords "final", "finally" and "finalize" have distinct roles. final enforces immutability and prevents changes to variables, methods, or classes. finally ensures a block of code runs after a try-catch, regardless of exceptions. finalize is a method used for cleanup before an object is
4 min read
Chained Exceptions in JavaChained Exceptions in Java allow associating one exception with another, i.e. one exception describes the cause of another exception. For example, consider a situation in which a method throws an ArithmeticException because of an attempt to divide by zero.But the root cause of the error was an I/O f
3 min read
Null Pointer Exception in JavaA NullPointerException in Java is a RuntimeException. It occurs when a program attempts to use an object reference that has the null value. In Java, "null" is a special value that can be assigned to object references to indicate the absence of a value.Reasons for Null Pointer ExceptionA NullPointerE
5 min read
Exception Handling with Method Overriding in JavaException handling with method overriding in Java refers to the rules and behavior that apply when a subclass overrides a method from its superclass and both methods involve exceptions. It ensures that the overridden method in the subclass does not declare broader or new checked exceptions than thos
4 min read
Java Advanced
Java Multithreading TutorialThreads are the backbone of multithreading. We are living in the real world which in itself is caught on the web surrounded by lots of applications. With the advancement in technologies, we cannot achieve the speed required to run them simultaneously unless we introduce the concept of multi-tasking
15+ min read
Synchronization in JavaIn multithreading, synchronization is important to make sure multiple threads safely work on shared resources. Without synchronization, data can become inconsistent or corrupted if multiple threads access and modify shared variables at the same time. In Java, it is a mechanism that ensures that only
10 min read
File Handling in JavaIn Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used to create an object of the class and then specifying the name of the file.Why File Handling is Required?File Handling is an integral part of any programming languag
6 min read
Java Method ReferencesIn Java, a method is a collection of statements that perform some specific task and return the result to the caller. A method reference is the shorthand syntax for a lambda expression that contains just one method call. In general, one does not have to pass arguments to method references.Why Use Met
9 min read
Java 8 Stream TutorialJava 8 introduces Stream, which is a new abstract layer, and some new additional packages in Java 8 called java.util.stream. A Stream is a sequence of components that can be processed sequentially. These packages include classes, interfaces, and enum to allow functional-style operations on the eleme
15+ min read
Java NetworkingWhen computing devices such as laptops, desktops, servers, smartphones, and tablets and an eternally-expanding arrangement of IoT gadgets such as cameras, door locks, doorbells, refrigerators, audio/visual systems, thermostats, and various sensors are sharing information and data with each other is
15+ min read
JDBC TutorialJDBC stands for Java Database Connectivity. JDBC is a Java API or tool used in Java applications to interact with the database. It is a specification from Sun Microsystems that provides APIs for Java applications to communicate with different databases. Interfaces and Classes for JDBC API comes unde
12 min read
Java Memory ManagementJava memory management is the process by which the Java Virtual Machine (JVM) automatically handles the allocation and deallocation of memory. It uses a garbage collector to reclaim memory by removing unused objects, eliminating the need for manual memory managementJVM Memory StructureJVM defines va
4 min read
Garbage Collection in JavaGarbage collection in Java is an automatic memory management process that helps Java programs run efficiently. Java programs compile to bytecode that can be run on a Java Virtual Machine (JVM). When Java programs run on the JVM, objects in the heap are created, which is a portion of memory dedicated
7 min read
Memory Leaks in JavaIn programming, a memory leak happens when a program keeps using memory but does not give it back when it's done. It simply means the program slowly uses more and more memory, which can make things slow and even stop working. Working of Memory Management in JavaJava has automatic garbage collection,
3 min read
Practice Java
Java Interview Questions and AnswersJava is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java Programs - Java Programming ExamplesIn this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Exercises - Basic to Advanced Java Practice Programs with SolutionsLooking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers
7 min read
Java Quiz | Level Up Your Java SkillsThe best way to scale up your coding skills is by practicing the exercise. And if you are a Java programmer looking to test your Java skills and knowledge? Then, this Java quiz is designed to challenge your understanding of Java programming concepts and assess your excellence in the language. In thi
1 min read
Top 50 Java Project Ideas For Beginners and Advanced [Update 2025]Java is one of the most popular and versatile programming languages, known for its reliability, security, and platform independence. Developed by James Gosling in 1982, Java is widely used across industries like big data, mobile development, finance, and e-commerce.Building Java projects is an excel
15+ min read