Java Articles

Page 115 of 450

What is the class "class" in Java?

seetha
seetha
Updated on 11-Mar-2026 614 Views

The Java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor. Example Following is the example demonstrates the usage of the class Class. The java.lang.Class.getCanonicalName() method returns the canonical name of the underlying class as defined by the Java Language Specification. It returns null if the class does not have a canonical name. import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = c.getClass(); ...

Read More

What is the difference between super and this, keywords in Java?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 11-Mar-2026 764 Views

The this is a keyword in Java which is used as a reference to the object of the current class. Using it you can − Differentiate the instance variables from local variables if they have same names, within a constructor or a method. Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation. Example class Superclass { int age; Superclass(int age) { this.age = age; } public void ...

Read More

How to execute a static block without main method in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

VM first looks for the main method (at least the latest versions) and then, starts executing the program including static block. Therefore, you cannot execute a static block without main method. Example public class Sample { static { System.out.println("Hello how are you"); } } Since the above program doesn’t have a main method, If you compile and execute it you will get an error message. C:\Sample>javac StaticBlockExample.java C:\Sample>java StaticBlockExample Error: Main method not found in class StaticBlockExample, please define the main method as: public static ...

Read More

What are annotations in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

Annotations are a tag (metadata) which provides info about a program. Annotations in Java Start with the symbol ‘@’. They are used by the compiler to detect errors. Software tools to generate code. They are used to show attributes of an element: e.g. @Deprecated, @Override. Annotation are used to describe the purpose of an element of the framework, e.g. @Entity, @TestCase, @WebService Annotations describe the behaviour of an element: @Statefull, @Transaction. Example class Sample{ public void display(){ System.out.println(" "); } } public class Test extends ...

Read More

What is static import in Java?

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 864 Views

As import statement allows to use a class without its package qualification, static import allows to access the static members of a class without class qualifications. For Example, to access the static methods you need to call the using class name − Math.sqrt(169); But, using static import you can access the static methods directly. Example import static java.lang.Math.*; public class Sample{ public static void main(String args[]){ System.out.println(sqrt(169)); } } Output 13.0

Read More

What is the keyword used in instantiating a class in Java?

seetha
seetha
Updated on 11-Mar-2026 1K+ Views

An object is created from a class. In Java, the new keyword is used to create new objects. There are three steps when creating an object from a class − Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object. Following is an example of creating an object − Example public class Puppy { public Puppy(String name) { ...

Read More

What is the difference between method hiding and method overriding in Java?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 2K+ Views

When super class and the sub class contains same instance methods including parameters, when called, the super class method is overridden by the method of the sub class. WIn this example super class and sub class have methods with same signature (method name and parameters) and when we try to invoke this method from the sub class the sub class method overrides the method in super class and gets executed. Example class Super{ public void sample(){ System.out.println("Method of the Super class"); } } public class MethodOverriding extends ...

Read More

What is method hiding in Java and how to use it?

Giri Raju
Giri Raju
Updated on 11-Mar-2026 2K+ Views

When super class and sub class contains same method including parameters and if they are static. The method in the super class will be hidden by the one that is in the sub class. This mechanism is known as method hiding. Example class Demo{ public static void demoMethod() { System.out.println("method of super class"); } } public class Sample extends Demo { public static void demoMethod() { System.out.println("method of sub class"); } public static void main(String args[] ) { Sample.demoMethod(); } } Output method of sub class

Read More

Why is method overloading not possible by changing the return type of the method only in java?

mkotla
mkotla
Updated on 11-Mar-2026 3K+ Views

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call. If you observe the following example, it contains two methods with same name, different parameters and if you call the method by passing two integer values the first method will be executed and, if you call by passing 3 integer values then the second method will be executed.It is not possible to decide to execute which method based on the return type, therefore, overloading is not possible just by changing the return type of the method. Example ...

Read More

How to find numbers in an array that are greater than, less than, or equal to a value in java?

Srinivas Gorla
Srinivas Gorla
Updated on 11-Mar-2026 4K+ Views

You can find numbers in an array that are greater than, less than, or equal to a value as:Examplepublic class GreaterOrLess {    public static void main(String args[]) {       int value = 65;       int[] myArray = {41, 52, 63, 74, 85, 96 };       System.out.println("Elements of the array that are equal to the given value are::");       for(int i = 0; i

Read More
Showing 1141–1150 of 4,498 articles
« Prev 1 113 114 115 116 117 450 Next »
Advertisements