Skip to main content

Posts

What is the difference between compile time error and run time error?

At compile time, when the code does not comply with the Java syntactic and semantics rules as described in Java Language Specification (JLS), compile-time errors will occurs. The goal of the compiler is to ensure the code is compliant with these rules. Any rule-violations detected at this stage are reported as compilation errors. The best way to get to know those rules is to go through all the sections in the JLS containing the key words "compile-time error". In general, these rules include syntax checking: declarations, expressions, lexical parsing, file-naming conventions etc; exception handling: for checked exceptions; accessibility, type-compatibility, name resolution: checking to see all named entities - variables, classes, method calls etc. are reachable through at least one of the declared path; etc. The following are some common compile time errors:

How does a static method access instance variables?

Static methods can not directly access any instance variables or methods. But they can access them by using their object reference. Static methods may even access private instance variables via a object reference.   public class Program { private int count; public Program(int ballcount){ count=ballcount; } public static void main(String argv[]){ Program s = new Program(99); //System.out.println(count); //compile time error //add(10); //compile time error System.out.println(s.count); s.add(10); System.out.println(s.count); }

What is the difference between implementing Runnable and extending Thread?

One difference between implementing Runnable and extending Thread is that by extending Thread , each of your threads has a unique object associated with it, whereas implementing Runnable , many threads can share the same object instance. For example, public class Program { public static void main (String[] args) { Runner r = new Runner(); Thread t1 = new Thread(r, "Thread A"); Thread t2 = new Thread(r, "Thread B"); Thread s1 = new Strider("Thread C"); Thread s2 = new Strider("Thread D"); t1.start(); t2.start(); s1.start(); s2.start(); } } class Runner implements Runnable { private int counter; public void run() { try { for (int i = 0; i != 2; i++) { System.out.println(Thread.currentThread().getName() + ": " + counter++); Thread.sleep(1000); } } catch(InterruptedException e) { e.printStackTrace(); } } }

Why the fully qualified name of a static final variable is not allowed in static initialization block?

Let's start with the following example:   public class Program { static final int var; static { Program.var = 8; // Compilation error } public static void main(String[] args) { System.out.println(Program.var); } } And, public class Program { static final int var; static { var = 8; //OK } public static void main(String[] args) { System.out.println(Program.var); } }

Can private method be overridden?

The private methods are not inherited by subclasses and you cannot be overridden by subclasses. According to Java Language Specification ( 8.4.8.3 Requirements in Overriding and Hiding ), "Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass." What does it mean? It means you can have a private method has the exact same name and signature as a private method in the superclass, but you are not overriding the private method in superclass and you are just declaring a new private method in the subclass. The new defined method in the subclass is completely unrelated to the superclass method. A private method of a class can be only ...

Why am I getting unreported exception when the super class default constructor has a 'throws' clause?

class Super { public Super() throws Exception { System.out.println("Super Class"); } } public class Sub extends Super { public static void main(String[] args) throws Exception { Sub s = new Sub(); } } Compile it and you have compile-time error: Sub.java:6: unreported exception java.lang.Exception in default constructor public class Sub extends Super { 1 error Here is a Sun's Bug Report which can answer this quesion: When a superclass constructor has a non-empty throws clause, subclasses must define an explicit constructor with an appropriate throws clause, as a default constructor has no throws clause. (This is stated in JLS 2e 8.8.7, ruling out the xxxxx alternative of copying the superclass constructor's throws clause.

Run time binding or compile time binding?

When we talk about the inheritance of class members, it's very important to remember when the binding occurs and what type they are bound to. Only overridden instance methods are bound at run time; and this kind of binding depends on the instance object type. For example: public class Parent { public void writeName() { System.out.println("Parent"); } } public class Child extends Parent { public void writeName() { System.out.println("Child"); } public static void main(String [] args) { Parent p = new Child(); p.writeName(); } } The output is : Child Instance variables, static variables, static overridden methods  and overloaded methods are all bound at compile time; and this kind of binding depends on the type of the reference variable and not on the object. For Example:   public class Parent { private static String age = "50"; private String hairColor = "grey"; public void writeN...