Finalize Method Invocation in Java

Narasimha Murthi
Updated on 29-Jun-2020 14:57:55

1K+ Views

The finalize() method belongs to the Object class. Right before closing an object, the garbage collector makes sure that there are no more references to it and, calls the finalize() method on it.Therefore, once you override the finalize() method in it, you can do all the cleanup activity like closing the resources like database connection, network connection, etc.protected void finalize throws Throwable{}It is invoked only once during the execution of a program.Following are some notable points about the finalize method.Since this method belongs the Object class, which is the super class of all the classes in java, you can override ... Read More

Set the Priority for a Thread in Android

Anvi Jain
Updated on 29-Jun-2020 14:57:41

880 Views

Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to use set the priority for a thread in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             In the above code, we have taken edittext and textview. When user enter some text into edittext, it ... Read More

Pass Objects as an Argument in Java

Narasimha Murthi
Updated on 29-Jun-2020 14:57:14

10K+ Views

Yes, you can pass objects as arguments in Java. Consider the following example: Here we have a class with name EmployeeExampleIn the following Java example, we a have a class with two instance variables name and age and a parameterized constructor initializing these variables.We have a method coypObject() which accepts an object of the current class and initializes the instance variables with the variables of this object and returns it.In the main method we are instantiating the Student class and making a copy by passing it as an argument to the coypObject() method. Live Demoimport java.util.Scanner; public class Student {   ... Read More

Use Multiple Threads in Android

Smita Kapse
Updated on 29-Jun-2020 14:57:05

3K+ Views

Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to use multiple threads in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.                 In the above code, we have taken edittext and textview. When user enter some text into edittext, it going to ... Read More

Override Equals Method in Java

Narasimha Murthi
Updated on 29-Jun-2020 14:56:35

3K+ Views

To compare two objects the object class provides a method with name equals(), this method accepts an object and compares it with the current object. If the references of these two objects are equal, then it returns true else this method returns false.ExampleIn the following example we have a class Employee with two variables name, age and a parameterized constructor.From the main method we are creating two objects by passing same values and, comparing both values using the equals() method.Since the equals() method of the Object class returns true only if the references of the two objects are equal, this ... Read More

Use Thread.sleep() in Android

Nishtha Thakur
Updated on 29-Jun-2020 14:56:28

5K+ Views

Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to use thread.sleep() in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             In the above code, we have taken edittext and textview. When user enter some text into edittext, it going to wait till 5000ms ... Read More

Create a Thread in Android

Anvi Jain
Updated on 29-Jun-2020 14:55:50

6K+ Views

Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to create a thread in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             In the above code, we have taken edittext and textview. When user enter some text into edittext, it going to wait till ... Read More

When is an Object Eligible for Garbage Collection?

Narasimha Murthi
Updated on 29-Jun-2020 14:55:12

2K+ Views

Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.When an object created in Java program is no longer reachable or used it is eligible for garbage collection.Following are some scenarios where a Java object could be unreachable/unused.Object inside a method − In Java a method is stored in the stack memory. When you call a method, JVM fetches it into the stack and executes it. After the execution it is popped out of the stack then, all its variables will be ... Read More

This Reference in Java

Narasimha Murthi
Updated on 29-Jun-2020 14:52:30

3K+ Views

The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.Using “this” you can −Differentiate the instance variables from local variables if they have same names, within a constructor or a method.class Student {    int age;    Student(int age) {       this.age = age;    } }Call one type of constructor (parametrized constructor or default) from other in a class. It is known as ... Read More

Serialize Static Variables in Java

Venkata Sai
Updated on 29-Jun-2020 14:48:49

5K+ Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).But, static variables belong to class therefore, you cannot serialize static variables in Java. Still if you try to do so, the program gets compiled successfully but it raises an exception at the time of execution.In the following java program, the class Student has a static variable and we are trying to serialize and desterilize its object in the another class named ExampleSerialize.Exampleimport java.io.FileInputStream; import java.io.FileOutputStream; ... Read More

Advertisements