Raja has Published 806 Answers

What is the importance of Runtime class in Java?

raja

raja

Updated on 31-Jul-2019 08:46:03

The java.lang.Runtime class is a subclass of Object class, can provide access to various information about the environment in which a program is running. The Java run-time environment creates a single instance of this class that is associated with a program. The Runtime class does not have any public constructors, so ... Read More

Why the java file name should be always the same as a public class name?

raja

raja

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

         In Java, the java file name should be always the same as a public class name.While writing a java program first it is saved as a ".java" file, when it is compiled it forms byte code which is a ".class" file as such that if we ... Read More

What are the differences between protected and default access specifiers in Java?

raja

raja

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

The Protected access specifier is visible within the same package and also visible in the subclass whereas the Default is a package level access specifier and it can be visible in the same package.Protected Access SpecifierProtected will acts as public within the same package and acts as private outside the package.Protected will also act as ... Read More

Can we access the instance variables from a static method in Java?

raja

raja

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

We cannot directly access the instance variables within a static method because a static method can only access static variables or static methods.An instance variable, as the name suggests is tied to an instance of a class. Therefore, accessing it directly from a static method, which is not tied to ... Read More

How to handle the ArithmeticException (unchecked) in Java?

raja

raja

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

The java.lang.ArithmeticException is an unchecked exception in Java. Usually, one would come across java.lang.ArithmeticException: / by zero which occurs when an attempt is made to divide two numbers and the number in the denominator is zero. ArithmeticException objects may be constructed by the JVM.Example1Live Demopublic class ArithmeticExceptionTest {    public static ... Read More

Is it possible to assign a numeric value to an enum in Java?

raja

raja

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

Yes, it's possible to assign a numeric value to an enum.Enum in Java is used to represent a list of predefined values. Most of the time, we probably not care about the associated value of an enum. However, there might be some times when we need to assign specific values ... Read More

Can an interface extend multiple interfaces in Java?

raja

raja

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

Yes, we can do it. An interface can extend multiple interfaces in Java.Example:interface A {    public void test();    public void test1(); } interface B {    public void test();    public void test2(); } interface C extends A, B {    public void test3(); } class D implements ... Read More

How do we use an enum type with a constructor in Java?

raja

raja

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

Enum type can have a private constructor that can be used to initialize instance fields. The EnumDemo class demonstrates this. It features a Food enum type with four constants: HAMBURGER, FRIES, HOTDOG, and ARTICHOKE. Notice that after each constant value is present in parentheses. This calls the constructor for that member ... Read More

Can we declare a top level class as protected or private in Java?

raja

raja

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

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier). If it does not have a modifier, it is supposed to have a default access.Syntax// A top level class    public class TopLevelClassTest {       // Class body }If ... Read More

Can we declare a try catch block within another try catch block in Java?

raja

raja

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

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.Nested Try-Catch BlockIf an inner try statement does not have a matching catch statement for a particular exception, the control is transferred to the next try statement catch handlers that are expected for a matching ... Read More

Advertisements