Java Articles

Page 113 of 450

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 772 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 848 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 905 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

How do I write class names in Java?

usharani
usharani
Updated on 11-Mar-2026 5K+ Views

While writing class names you need to keep the following points in mind. You shouldn’t use predefined or existing class names as the name of the current class. You shouldn’t use any Java keywords as class name (with the same case). The First letter of the class name should be capital and remaining letters should be small (mixed case). class Sample Likewise, first letter of each word in the name should be capital an remaining letters should be small. class Test Keeping interface names simple and descriptive is suggestable. Better not ...

Read More

How do I write variable names in Java?

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

While choosing an identifier to declare a variable in Java you need to keep the following points in mind. The name of the variable should begin with either alphabet or, an underscore (_) or, a dollar ($) sign. The identifiers used for variables must not be keywords. No spaces or special characters are allowed in the variable names of Java. Variable names may contain 0 to 9 numbers (if not at the beginning). Variable names are case sensitive i.e. MY_NUM is different from my_num. If you use two words in a identifier then the you should follow camel case ...

Read More

Can you have a method in Java with varying number of arguments?

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

Yes, we can write a method using variable arguments once you use variable arguments as a parameter method while calling you can pass as many numbers 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) {       for (String arg : args) {          System.out.println(arg);       }    }    public static void main(String args[] ){       new Sample().demoMethod("ram", "rahim", "robert");       new Sample().demoMethod("krishna", "kasyap");       new Sample().demoMethod();    } }Outputram rahim robert krishna kasyap

Read More

How do I write interface names in Java?

mkotla
mkotla
Updated on 11-Mar-2026 747 Views

While writing an interface/class names you need to keep the following points in mind. First letter of the interface/class name should be capital and remaining letters should be small (mixed case). interface Sample Likewise, first letter of each word in the name should be capital an remaining letters should be small. interface MYInterface Keeping interface names simple and descriptive is suggestable. Better not to use acronyms while writing interface/class names. Example interface MyInterface { void sample(); void demo(); } public class Test ...

Read More

How do I write constants names in Java?

vanithasree
vanithasree
Updated on 11-Mar-2026 788 Views

While writing the name of the constants it is suggested to write all the letters in upper case. If constant contains more than one word they should be separated by underscore (_). Example public class ConstantsTest { public static final int MIN_VALUE = 22; public static final int MAX_VALUE = 222; public static void main(String args[]) { System.out.println("Value of the constant MIN_VALUE: "+MIN_VALUE); System.out.println("Value of the constant MAX_VALUE: "+MAX_VALUE); } } Output Value of the constant MIN_VALUE: 22 Value of the constant MAX_VALUE: 222

Read More
Showing 1121–1130 of 4,498 articles
« Prev 1 111 112 113 114 115 450 Next »
Advertisements