Programming Articles

Page 2544 of 2544

Do local variables in Java have default values?

Ali
Ali
Updated on 30-Jul-2019 1K+ Views

No, local variables do not have default values. Once we create a local variable we must initialize it before using it. Since the local variables in Java are stored in stack in JVM there is a chance of getting the previous value as default value. Therefore, In Java default values for local variables are not allowed. Example public class Sample { public static void main(String args[] ){ int data; System.out.println(data); } } Error C:\Sample>javac Sample.java Sample.java:4: error: variable data might not have been initialized System.out.println(data); ^ 1 error

Read More

What is a method signature in Java?

Swarali Sree
Swarali Sree
Updated on 30-Jul-2019 8K+ Views

The method signature consists of the method name and the parameter list. Example Live Demo public class MethodSignature { public int add(int a, int b){ int c = a+b; return c; } public static void main(String args[]){ MethodSignature obj = new MethodSignature(); int result = obj.add(56, 34); System.out.println(result); } } Output 90 Method ...

Read More

Can static variables be called using className.variableName in Java?

Rahul Sharma
Rahul Sharma
Updated on 30-Jul-2019 2K+ Views

Class variables also known as static variables are declared with the static keyword There would only be one copy of each class variable per class, regardless of how many objects are created from it. You can access a class variable without instantiation using the class name as className.variableName. Example Live Demo public class Test{ static int num = 92; public static void main(String args[]) throws Exception { System.out.println(Test.num); } } Output 92

Read More
Showing 25431–25433 of 25,433 articles
« Prev 1 2540 2541 2542 2543 2544 Next »
Advertisements