Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 40 of 450
Why variables are declared final in Java
A variable cannot be modified after it is declared as final. In other words, a final variable is constant. So, a final variable must be initialized and an error occurs if there is any attempt to change the value.A program that demonstrates a final variable in Java is given as follows −Examplepublic class Demo { public static void main(String[] args) { final double PI = 3.141592653589793; System.out.println("The value of pi is: " + PI); } }OutputThe value of pi is: 3.141592653589793Now let us understand the above program.In the main() method in class ...
Read MoreClass declaration with a method that has a parameter in Java
A class declaration can contain a method that has a parameter in Java. A program that demonstrates this is given as follows:Exampleclass Message { public void messagePrint(String msg) { System.out.println("The message is: " + msg); } } public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint("Java is fun"); } }OutputThe message is: Java is funNow let us understand the above program.The Message class is created with a single member function ...
Read MoreDuplicating Objects using a Constructor in Java
A copy constructor can be used to duplicate an object in Java. The copy constructor takes a single parameter i.e. the object of the same class that is to be copied. However, the copy constructor can only be explicitly created by the programmer as there is no default copy constructor provided by Java.A program that demonstrates this is given as follows −Exampleclass NumberValue { private int num; public NumberValue(int n) { num = n; } public NumberValue(NumberValue obj) { num = obj.num; } public void display() { ...
Read MorePass long parameter to an overloaded method in Java
Method overloading in a class contains multiple methods with the same name but the parameter list of the methods should not be the same. One of these methods can have a long parameter in their parameter list.A program that demonstrates this is given as follows −Exampleclass PrintValues { public void print(int val) { System.out.println("The int value is: " + val); } public void print(long val) { System.out.println("The long value is: " + val); } } public class Demo { public static void main(String[] args) { PrintValues ...
Read MoreMethods Accepting a Variable Number of objects in Java
A method that accepts a variable number of Object arguments in Java is a form of variable length arguments(Varargs). These can have zero or multiple Object type arguments.A program that demonstrates this is given as follows:Examplepublic class Demo { public static void Varargs(Object... args) { System.out.println("Number of Object arguments are: " + args.length); System.out.println("The Object argument values are: "); for (Object i : args) System.out.println(i); } public static void main(String args[]) { Varargs("Apples", "4", "All"); Varargs("Half of", 3, "is", ...
Read MoreHow to extend Interfaces in Java
An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. An interface extends another interface like a class implements an interface in interface inheritance.A program that demonstrates extending interfaces in Java is given as follows:Exampleinterface A { void funcA(); } interface B extends A { void funcB(); } class C implements B { public void funcA() { System.out.println("This is funcA"); } public void funcB() { System.out.println("This is funcB"); } } public class Demo { public ...
Read MoreA static initialization block in Java
Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded. There can be multiple static initialization blocks in a class that is called in the order they appear in the program.A program that demonstrates a static initialization block in Java is given as follows:Examplepublic class Demo { static int[] numArray = new int[10]; static { System.out.println("Running static initialization block."); for (int i = 0; i < numArray.length; i++) { ...
Read MoreDemonstrate static variables, methods and blocks in Java
The static variable is a class level variable and it is common to all the class objects i.e. a single copy of the static variable is shared among all the class objects.A static method manipulates the static variables in a class. It belongs to the class instead of the class objects and can be invoked without using a class object.The static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded.A program that demonstrates this is given as follows:Examplepublic class Demo { static int x = 10; static ...
Read MoreReference static field after declaration in Java
The static field variable is a class level variable and it belongs to the class not the class object.So the static field variable is common to all the class objects i.e. a single copy of the static field variable is shared among all the class objects.A program that demonstrates referencing the static field variable after declaration is given as follows:Examplepublic class Demo { static int a = 7; static int b = a + 5; public static void main(String[] args) { System.out.println("a = " + a); System.out.println("b = " + b); ...
Read MoreHow to use the asList() method of the Arrays class to create a Vector object
A Vector can be created from an Array using the java.util.Arrays.asList() method.A program that demonstrates this is given as follows:Exampleimport java.util.Arrays; import java.util.Vector; public class Demo { public static void main(String args[]) { Integer[] arr = { 3, 1, 9, 6, 4, 8, 7 }; Vector vec = new Vector(Arrays.asList(arr)); System.out.println("The Vector elements are: " + vec); } }OutputThe Vector elements are: [3, 1, 9, 6, 4, 8, 7]Now let us understand the above program.The Integer Array arr[] is defined. Then a Vector is created using the Arrays.asList() ...
Read More