Found 9150 Articles for Object Oriented Programming

How to check whether a key exist in JavaScript object or not?

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

328 Views

There are a couple of ways to find whether a key exist in javascript object or not.Let's say we have an 'employee' object as shown below.   var employee = {       name: "Ranjan",       age: 25    }Now we need to check whether 'name' property exist in employee object or not.1) 'In' operatorWe can use 'in' operator on object to check its properties. The 'in' operator also looks about inherited property if it does not find any actual properties of the object.In the following example when 'toString' is checked whether present or not, the 'in' operator ... Read More

How to empty an array in JavaScript?

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

848 Views

There are a couple of ways to empty an array in javascript.Let's suppose take an arrayvar array1 = [1, 2, 3, 4, 5, 6, 7];Method 1var array1 = [];The code above will set the number array to a new empty array. This is recommended when you don't have any references to the original array 'array1'. You should be careful with this way of empty the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.Example var array1 = [1, 2, 3, 4, 5, 6, 7];  // ... Read More

How to instantiate member inner class in Java?

raja
Updated on 11-Feb-2020 09:21:14

2K+ Views

A class that is declared inside a class but outside a method is known as member inner class.We can instantiate a member Inner class in two waysInvoked within the classInvoked outside the classRules for Inner ClassThe outer class (the class containing the inner class) can instantiate as many numbers of inner class objects as it wishes, inside its code.If the inner class is public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.No inner class objects are automatically instantiated with an outer class object.If the inner ... Read More

What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?

Shriansh Kumar
Updated on 16-May-2025 18:44:03

5K+ Views

In Java, both ClassNotFoundException and NoClassDefFoundError are issues that occur when the JVM or ClassLoader is not able to find the appropriate class at the time of loading (run-time). The ClassNotFoundException is a checked exception, and NoClassDefFoundError is an Error that comes under unchecked. There are different types of ClassLoaders, each responsible for loading classes from different sources such as directories, JAR files, or network locations. If a required class is missing due to an incorrect classpath or a missing JAR file, the ClassLoader might fail to load it. This situation leads to one of these two issues. The ClassNotFoundException in ... Read More

Describe pass by value and pass by reference in JavaScript?

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

2K+ Views

Pass by value In pass by value, a function is called by directly passing the value of the variable as the argument. Changing the argument inside the function doesn’t affect the variable passed from outside the function. Javascript always pass by value so changing the value of the variable never changes the underlying primitive (String or number).In the following example, variable 'a' has assigned value 1. But inside function 'change' it got assigned with value 2. Since javascript is always a pass by value, the displayed output will be '1' but not '2'.ExampleLive Demo    let a = 1;   ... Read More

How to merge properties of two JavaScript Objects dynamically?

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

319 Views

There are two methods to merge properties of javascript objects dynamically. They are1) Object.assign() The Object.assign() method is used to copy the values of all properties from one or more source objects to a target object. It will return the target object.Example-1 Live Demo var target = { a: "ram", b: "rahim" }; var source = { c: "akbar", d: "anthony" }; var returnedTarget = Object.assign(target, source); document.write(JSON.stringify(target)); document.write(JSON.stringify(returnedTarget)); output{"a":"ram", "b":"rahim", "c":"akbar", "d":"anthony"} {"a":"ram", "b":"rahim", "c":"akbar", "d":"anthony"}If the objects have the same keys, ... Read More

How to rethrow an exception in Java?

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

19K+ Views

Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the enclosing try block have an opportunity to catch the exception.Syntaxcatch(Exception e) {    System.out.println("An exception ... Read More

What is the importance of the Throwable class and its methods in Java?

raja
Updated on 06-Feb-2020 11:30:26

517 Views

The Throwable class is a superclass of all errors and exceptions in Java. Objects that are instances of this class are thrown by the Java Virtual Machine or can be thrown by a throw statement. Similarly, this class or one of its subclasses can be the argument type in a catch clause.Instances of two subclasses Error and Exception are used to indicate that exceptional situations have occurred, these instances are created in the context of the exceptional situation to include relevant information.Commonly used exception methods of Throwable classpublic String getMessage(): returns the message string about the exception.public Throwable getCause(): returns the cause of the exception. ... Read More

What is non-enumerable property in JavaScript and how can it be created?

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

3K+ Views

Non-enumerable propertyObjects can have properties that don't show up when iterated through the particular object using Object.keys() or for...in loop.Those type of properties are called as non-enumerable properties.Creating a non-enumerable propertyTo create a non-enumerable property we have to use Object.defineProperty() method. This a special method to create non-enumerable properties in an object. In the following example, three properties such as name, age and country were created normally and a property named "salary" was created using Object.defineProperty() method and key named enumerable was assigned with false. When the object "person" got iterated using Object.keys() the properties such as name, age and country ... Read More

How to prevent modification of object in JavaScript ?.

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

389 Views

ECMAScript 5 has introduced several methods to prevent modification of object. Those preventive measures ensures that no one, accidentally or otherwise change functionality of object.There are 3 levels of preventive methods1) Prevent ExtensionsIn this level, one cannot add any new properties or methods but can access existing properties or methods. Here there is an ability to delete the respective object. Object.preventExtensions() is the method used to accomplish this task. It prevents any new properties from ever being added to the object.ExampleLive Demo var object1 = { prop1: 1 ... Read More

Advertisements