Found 9119 Articles for Object Oriented Programming

What does the modifier transient in Java do?

George John
Updated on 30-Jul-2019 22:30:20

239 Views

An instance variable is marked transient to point the JVM to skip the actual variable once serializing the thing containing it. This modifier is included in the statement that creates the variable, preceding the class or data type of the variable. Example public class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Mailing a check to " + name + " ... Read More

Can access modifiers be used for local variables in Java?

Arushi
Updated on 30-Jul-2019 22:30:20

785 Views

Yes, a local variable can be public, private, protected or default.

How to use the final modifier in Java?

Moumita
Updated on 25-Oct-2024 23:40:58

1K+ Views

In this article, we will learn to use the final modifier in Java. The final modifier can be associated with methods, classes, and variables. Once we declare it final − A final class cannot be instantiated. A final method cannot be overridden. A final variable cannot be reassigned. These restrictions make code more predictable and can prevent unintentional modifications. Steps to use the final modifierFollowing are the steps to use the final modifier −First, we will define a class TestExample and declare a final variable value ... Read More

What does abstract modifier in Java do?

Vikyath Ram
Updated on 30-Jul-2019 22:30:20

360 Views

An abstract keyword is used to declare methods abstract methods and abstract classes. Once a method is declared abstract we should not specify body for those. Example public abstract class Sample{ public abstract demo(){ } } Error Sample.java:2: error: invalid method declaration; return type required public abstract demo(){ ^ 1 error And once a class is declared abstract it cannot be instantiated. Example public abstract class Sample{ public abstract void demo(); public static void main(String args[]){ new Sample(); } } Error C:\Sample>javac Sample.java Sample.java:4: error: Sample is abstract; cannot be instantiated new Sample(); ^ 1 error

What does synchronized modifier do in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:20

1K+ Views

The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers. Example Live Demo public class TestThread { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String args[]) { ThreadDemo1 T1 = new ThreadDemo1(); ThreadDemo2 T2 = new ThreadDemo2(); ... Read More

Can a Java array be declared as a static field, local variable or a method parameter?

Rahul Sharma
Updated on 30-Jul-2019 22:30:20

613 Views

We can declare an array as a local variable or method parameter but, an array cannot be static. Example public class Test{ public void sample(){ static int[] myArray = {20, 30}; System.out.println(); } public static void main(String args[]){ Test t = new Test(); t.sample(); } } Error C:\Sample>javac Test.java Test.java:3: error: illegal start of expression static int[] myArray = {20, 30}; ^ 1 error

What are final, abstract, synchronized non-access modifiers in Java?

Jai Janardhan
Updated on 30-Jul-2019 22:30:20

431 Views

The abstract keyword is used to declare methods abstract methods and abstract classes. Once a method is declared abstract we should not specify body for those. And once a class is declared abstract it cannot be instantiated. Example Live Demo abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; ... Read More

What is a final parameter in java?

Alshifa Hasnain
Updated on 26-Mar-2025 12:04:15

4K+ Views

In this article, we will learn about the final parameter in Java. In Java, you can pass final variables as parameters to methods. The final keyword can be used with variables, methods, and classes to add restrictions on their behavior. What is a Final Parameter? A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed. So, the object's state can be changed but not the reference. With variables, the final modifier often is used with static to ... Read More

What is the scope of private access modifier in Java?

George John
Updated on 30-Jul-2019 22:30:20

1K+ Views

The scope of the private modifier lies with in the class. Members that are declared private cannot be accessed outside the class. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Variables that are declared private can be accessed outside the class, if public getter methods are present in the class. Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world. Example The following class uses private access control public class Logger { private String format; ... Read More

What is a blank uninitialized final variable in java?

Amit Sharma
Updated on 02-Sep-2022 13:31:00

1K+ Views

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable. Therefore, once we declare a final variable it is mandatory to initialize the final variable at the time of declaration or using constructor. If not, a compile time error may occur saying “The blank final field num ... Read More

Advertisements