Programming Articles

Page 1582 of 2547

What are method local inner classes in Java?

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

In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted to the method.A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class.Examplepublic class OuterClass {    public void display(){       int num = 23;       class Inner{          public void print() {             System.out.println("This is method inner class "+num);       ...

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

Can we define an interface inside a Java class?

Sravani S
Sravani S
Updated on 11-Mar-2026 7K+ Views

Yes, you can define an interface inside a class and it is known as a nested interface. You can’t access a nested interface directly; you need to access (implement) the nested interface using the inner class or by using the name of the class holding this nested interface.Examplepublic class Sample {    interface myInterface {       void demo();    }    class Inner implements myInterface {       public void demo() {          System.out.println("Welcome to Tutorialspoint");       }    }    public static void main(String args[]) {       Inner obj = ...

Read More

Does Java support default parameter values for a method?

Sravani S
Sravani S
Updated on 11-Mar-2026 3K+ Views

Java does not support the concept of default parameter however, you can achieve this usingMethod overloadingUsing method overloading if you define method with no arguments along with parametrized methods. Then you can call a method with zero arguments.Variable argumentsIn Java methods parameters accept arguments with three dots. These are known as variable arguments. Once you use variable arguments as a parameter method, while calling you can pass as many number of arguments to this method (variable number of arguments) or, you can simply call this method without passing any arguments.Examplepublic class Sample {    void demoMethod(String... args) {     ...

Read More

Can you extend a static inner class in Java?

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

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class. You can extend static inner class with another inner class. Example public class SampleClass { static abstract class Test{ int num = 300; public abstract void display(); } ...

Read More

Are static methods inherited in Java?

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

The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. We can inherit static methods in Java. Example In the example we are creating a class named Demo and, declared a static method named display(). We created another class Sample, extended the Demo class and tried to access the display() method using the sub ...

Read More

Is there a need to import Java.lang package while running Java programs?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 774 Views

The java.lang package is the default package in Java, by default, it will be imported. Therefore, there is no need to import this package explicitly. i.e. without importing you can access the classes of this package. Example If you observe the following example here we haven’t imported the lang package explicitly but, still, we are able to calculate the square root of a number using the sqrt() method of the java.lang.Math class. public class LangTest { public static void main(String args[]) { int num = 100; ...

Read More

How to write single line comment in Java?

mkotla
mkotla
Updated on 11-Mar-2026 623 Views

To comment a particular line just place ‘double back slash (//)’ before the line as shown below. // Hello this line is commented Example Following example demonstrates the usage of single line comments in Java. public class CommentsExample { public static void main(String args[]) { //Declaring a variable named num int num = 1; //Printing the value of the variable num System.out.println("value if the variable num: "+num); } } Output value if the variable num: 1

Read More

What is the difference between /* */ and /** */ comments in Java?

vanithasree
vanithasree
Updated on 11-Mar-2026 853 Views

Multiline comments (/* */) are used to comment multiple lines in the source code. Example public class CommentsExample { /* Following is the main method here, We create a variable named num. And, print its value * */ public static void main(String args[]) { //Declaring a variable named num int num = 1; ...

Read More

What are valid identifiers in Java?

seetha
seetha
Updated on 11-Mar-2026 908 Views

A valid identifier in java – Must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). Can have any combination of characters after the first character. Cannot be a keyword. Example Following example shows various possible identifiers used to declare a variable in Java. public class VariableTest { public static void main(String args[]) { // Declaring a variable named num int num = 1; int _num = ...

Read More
Showing 15811–15820 of 25,466 articles
Advertisements