inner class inside method
Yes, we can have an inner class inside a method and final variables can be accessed.
Yes, we can have an inner class inside a method and final variables can be accessed.
Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.
final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.
Any exception that extends from RuntimeException is considered an unchecked exception. Unchecked exceptions do not have to be handled in a catch block, though they can be. The compiler never forces you to handle unchecked exceptions.
An exception that the compiler forces code to handle. You can usually accomplish this by using a try/catch block. All checked exceptions inherit from the java.lang.Exception class but do not extend the java.lang.RuntimeException class.
Whenever you create your own exceptions, you follow some common steps.
Technically, an abstract class does not have to contain any abstract methods. This is rather unusual, but in such a case, the class simply cannot be instantiated.
The first line of every constructor must call another constructor. If you do not explicitly call another constructor with this() or super(), he compiler always makes super() with no parameters the first line of a constructor.
When you are performing object casting, the best practice is to incorporate the instanceof operator into your code. First, use instanceof to be sure that the target object is the type you expect; only then should you perform the cast. This ensures that you are never trying to cast an object illegally, and you can be sure to avoid those pesky runtime errors.
Although methods can take advantage of virtual method invocation, variables cannot. Variables are always bound to the reference type. This is important to understand if you provide the same variable name in both a superclass and a subclass.
If you define a variable in your subclass with the same name as a variable in your superclass, the superclass variable is hidden. This can lead to hard-to-find errors, so be careful about doing this. The JVM always uses the variable that is the “most local” to the object. If a variable x is defined in both the superclass and the subclass, a subclass instance “sees” only its own variable.
Constructors can be “chained” together, allowing one constructor to invoke another and so on until an “ultimate constructor” executes. To chain constructors, you use the this keyword to represent the constructor invocation.
Static methods cannot access instance methods or instance variables:
The truth is that everything—both objects and primitive types—is passed by value. For a primitive type, the value is whatever was assigned to the primitive before it was passed. For an object, the value is the actual object reference. All object references in the JVM are just 32-bit integers that the JVM maps to memory addresses.
The switch statement can process only four primitive types: byte, short, char, and int. No other types are allowed for evaluation in a switch statement. Although char may not seem to be an integer, it is. The value it actually holds is the Unicode value, which is a 16-bit integer value. It is represented in code normally as an actual character, of course.
String title = “Java”;
The Math class is final and all the methods defined in the Math class are static, which means you cannot inherit from the Math class and override these methods. Also, the Math class has a private constructor, so you cannot instantiate it.
The static keyword is needed in the main () so that the interpreter can access the method right from the class instead of requiring an object. As you know of objects so far, you should know that normally a class is just a template for individual objects. A nonstatic method can be called only if an object has been created from a class. A static method can be called without any objects being created.
Integer i= new Integer("10");
For char literals, a single character is enclosed in single quotes. You can also use the prefix \u followed by four hexadecimal digits representing the 16-bit Unicode character: '\u004E', 'A', 'a'.
Octal literals begin with zero and only digits 0 through 7 are allowed.
Remember that whenever you pass an object to the System.out.println() method, the object’s toString() method is automatically called. Because of polymorphism, this method is always called on the runtime type of the object, which in this case is a String.
An assertion is a statement containing a boolean expression that is assumed to be true when the statement is executed. The system reports an AssertionError if the expression evaluates to false. It is used for debugging purposes:
When we declare an array variable, the code creates a variable that can hold the reference to an array object. It does not create the array object or allocate space for array elements. It is illegal to specify the size of an array during declaration. The square brackets may appear as part of the type at the beginning of the declaration or as part of the array identifier:
Following are the few methods in Runtime class -
Process exec(String[] cmdarray) --> Executes the specified command and arguments in a separate process.
Process exec(String[] cmdarray, String[] envp) --> Executes the specified command and arguments in a separate process