Found 9150 Articles for Object Oriented Programming

Is it possible to assign a reference to "this" in java?

Maruthi Krishna
Updated on 29-Jun-2020 12:21:45

1K+ Views

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using it, you can refer the members of a class such as constructors, variables, and methods.Assigning reference to "this"According to the definition "this" is a keyword which acts as a reference to the current object (the object from whose constructor/method you are using it), its value id is fixed. Therefore, you cannot assign a new reference value to it. Moreover, it is just a keyword, not a variable.Still, if you try to it assign a reference value to "this" it ... Read More

Explain the basic structure of a program in Java?

Alshifa Hasnain
Updated on 11-Apr-2025 13:59:18

6K+ Views

In this article, we will learn about the basic structure of a program in Java. Java is widely used for developing large-scale applications, including Android apps, web applications, and enterprise software. To write a Java program, it's essential to understand its basic structure, which consists of several key components. Components A typical structure of a Java program contains the following elements: Package declaration Import statements Comments Class definition Class variables, Local variables Methods/Behaviors ... Read More

How to hide/show HTML elements in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

Using Css style we can hide or show HTML elements in javascript. Css provides properties such as block and none to hide/show the HTML elements.Hiding an elementExampleIn the following example when the "Hideme" button has clicked the text in the paragraph tag has been disappeared as shown in the output.Live Demo    Using JavaScript to hide HTML elements.    

What is the order of execution of non-static blocks with respect to a constructor in Java?

Alshifa Hasnain
Updated on 26-Aug-2025 16:34:46

3K+ Views

In Java, non-static blocks run before the constructor whenever an object is created. If multiple non-static blocks exist, they execute in the order they appear in the class, followed by the constructor. In this article, we will understand this order of execution with the help of examples. What Are Non-Static Blocks in Java? The Non-static blocks are class-level blocks which don't contain a prototype. A non-static block must perform any logic whenever an object is instantiated, regardless of the constructor. The Non-static blocks are automatically called by the JVM for every object creation in the Java stack area. We can ... Read More

How to use the substring() method of java.lang.String class in Java?

raja
Updated on 07-Feb-2020 05:41:38

235 Views

The substring() method returns a String datatype which corresponds to the original String starting from the begin index until the end Index. If the end index is not specified, it is imperative that the endIndex is the String length. Since we are dealing with the String, the index starts at '0' position.Syntaxpublic String substring(int beginIndex) public String substring(int beginIndex, int endIndex)beginIndex: the starting index or position where we want to start to cut or substring our String.endIndex:    the end index or position where we want to end our cutting or substring our String.This method returns a String datatype which corresponds to the ... Read More

How to handle the exception using UncaughtExceptionHandler in Java?

raja
Updated on 30-Jul-2019 22:30:26

1K+ Views

The UncaughtExceptionHandler is an interface inside a Thread class. When the main thread is about to terminate due to an uncaught exception the java virtual machine will invoke the thread’s UncaughtExceptionHandler for a chance to perform some error handling like logging the exception to a file or uploading the log to the server before it gets killed. We can set a Default Exception Handler which will be called for the all unhandled exceptions. It is introduced in Java 5 Version.This Handler can be set by using the below static method of java.lang.Thread class.public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler ueh)We have to provide an implementation of the interface ... Read More

How to change/increase the heap size of the Java Virtual Machine in Java?

Alshifa Hasnain
Updated on 24-Apr-2025 14:27:38

1K+ Views

A Java program can execute in the Java Virtual Machine (JVM) and uses the heap memory to manage the data. If our Java program requires more memory, there is a possibility that the Java Virtual Machine(JVM) will begin to throw OutOfMemoryError instances when attempting to instantiate an object in Java. How to change/increase the JVM Heap Size? In Java, it is possible to increase the heap size allocated by the JVM: -Xms: Set an initial Java heap size JVM automatically manages the heap memory but we can ... Read More

How to find duplicates in an array using set() and filter() methods in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

1K+ Views

Removing duplicatesTo remove duplicates in an array we have many logical methods, but advanced javascript has provided some methods so that the task of removing duplicates has become very simple. Some of those methods are set() and filter(). For better understanding lets' discuss each method individually.Set()The important use of the set() method is that it only allows unique values. In other words, it will automatically remove duplicates and makes the task easy for us. The Set() method won't take any logical approach in removing duplicates.Example In the following example, the duplicates in the provided array have been removed without any ... Read More

How to convert a string in to a function in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 11:57:31

6K+ Views

To convert a string in to function "eval()" method should be used. This method takes a string as a parameter and converts it into a function.syntaxeval(string);ExampleIn the following example, in a string itself, a property called 'age' is assigned with a function. Later on, using eval() function the property age is converted into a function and displayed as shown in the output. Live Demo    var string = '{"name":"Ram", "age":"function() {return 27;}", "city":"New jersey"}';    var fun = JSON.parse(string);    fun.age = eval("(" + fun.age + ")");    document.write(fun.name + " "+ "of Age" + " "+ fun.age()+ " " + "from ... Read More

What is traits in PHP?

Alok Prasad
Updated on 08-Nov-2023 10:43:18

3K+ Views

In 5.4 PHP version trait is introduced to PHP object-oriented programming. A trait is like class however it is only for grouping methods in a fine-grained and reliable way. It isn't permitted to instantiate a trait on its own. Traits are introduced to PHP 5.4 to overcome the problems of single inheritance. As we know in single inheritance class can only inherit from one other single class. In the case of trait, it enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Example Output Result of addition two numbers:8 ... Read More

Advertisements