Declaration, Initialization and Scoping for Java

The following is a summary for showing Java declaration, initialization and scoping.

  1. Variables defined in try block are not visible in catch block.
  2. You cannot pass parameters when you implement an interface by an anonymous class.
  3. Constructors cannot return anything. Not even void.
  4. Every enum has a values() method that returns an array of all the values in that enum in the order they are defined.
  5. System.out.println(0x10 + 10 + 010); Hexadecimal values can be denoted by prefixing with 0x ( Zero and upper or lower case ‘x ‘) so 0x10 is equivalent to 16 decimal. Octal values can be denoted by prefixing with 0 ( Zero ) so 010 is equivalent to 8 decimal. The whole expression will be evaluated to 34 ( 16+10+8).
  6. Variables cannot be declared synchronized. Only methods can be declared synchronized.
  7. Any class can be declared abstract.
  8. Non static methods can access static as well as non static methods of a class.
  9. Only one public class can be defined in a file.
  10. To construct a Base class, it’s super class needs to be constructed first. So some constructor of the super class has to be called. Either you explicitly call it or the compiler will add super() (ie. no args constructor) as the first line of the sub class constructor. Now, if the super class does not have a no-args constructor, the call super() will fail. Instead of calling super(…) you can also call another constructor of the base class in the first line.
  11. Unlike methods, a constructor cannot be abstract, static, final, native, or synchronized. A constructor is not inherited, so there is no need to declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java Virtual Machine to verify that superclass constructors are always properly invoked during object creation.
  12. The construct ‘{ }’ is a compound statement. The compound statement can contain zero or more arbitrary statements. Thus, { { } }, which is a compound statement containing one statement which is a compound statement containing no statement, is legal.
  13. ‘static’ and ‘final’ are valid modifiers for both ‘variable’ and ‘method’ declarations within a class.
  14. ‘transient’ and ‘volatile’ modifiers are only valid for ‘variables’.
  15. ‘abstract’ and ‘native’ are only valid for ‘methods’.
  16. Note: a class can have only have ‘final’, ‘abstract’ and ‘public’ as modifiers.
  17. A class can be extended unless it is declared final. While declaring a method, static usually implies that it is also final, this is not true for classes.
  18. An inner class can be declared static and still be extended. Notice the distinction. For classes, final means it cannot be extended, while for methods, final means it cannot be overridden in a subclass.
  19. The native keyword can only be used on methods, not on classes and instance variables.

13 thoughts on “Declaration, Initialization and Scoping for Java”

  1. Pingback: sales trainer cork
  2. Pingback: sales trainer tips

Leave a Comment