Showing posts with label 310-065. Show all posts
Showing posts with label 310-065. Show all posts

Saturday, April 16, 2011

SCJP Exam Watch 11 - Access instance variable/method from static mathod

One of the mistakes most often made by new Java programmers is attempting to access an instance variable (which means nonstatic variable) from the static main() method (which doesn't know anything about any instances, so it can't access the variable). The following code is an example of illegal access of a nonstatic variable from a static method:

class Foo {
    int x = 3;
    public static void main(String[] args) {
        system.out.println("x is " + x);
    }
}

Understand that this code will never compile, because you can't access a nonstatic (instance) variable from a static method. Just think of the compiler saying, "Hey, I have no idea which Foo object's x variable you're trying to print!". Remember, it's the class running the main() method, not an instance of the class.

Of course, the tricky part for the exam is that the question won't look as obvious as the preceding code. The problem you're being tested for accessing a nonstatic variable from a static method will be buried in code that might appear to be testing something else. For example, the preceding code would be more likely to appear as

class Foo {
    int x = 3;
    float y = 4.3f;
    public static void main(String[] args) {
        for (int z - x; z < ++x; z--, y = y + z) {
            // complicated looping and branching code
        }
    }
}
So, while you're trying to follow the logic, the real issue is that x and y can't be used within main(), because x and y are instance, not static, variables! The same applies for accessing nonstatic methods from a static method. The rule is, a static method of a class can't access a nonstatic (instance) method or variable of its own class.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Sunday, April 10, 2011

SCJP Exam Watch 10 - Methods return an abstract class or interface

Watch for methods that declare an abstract class or interface return type, and know that any object that passes the IS-A test (in other words, would test true using the instanceof operator) can be returned from that method - for example:

public abstract class Animal {}
public class Bear extends Animal {}
public class Test {
    public Animal go () {
        return new Bear();    // OK, Bear "is-a" Animal
    }
}

This code will compile, the return value is a subtype.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Saturday, August 28, 2010

SCJP Exam Watch 9 - Class and Interface Declaration

Look for illegal uses of extends and implements.  The following shows examples of legal and illegal class and interface declarations:


class Foo {}                        // OK
class Bar implements Foo {}         // No! Can't implement a class
interface Baz {}                    // OK
interface Fi {}                     // OK
interface Fee implements Baz {}     // No! Interface can't implement an interface
interface Zee implements Foo {}     // No! Interface can't implement a class
interface Zoo extends Foo {}        // No! Interface can't extend a class
interface Boo extends Fi {}         // OK
class Toon extends Foo, Button {}   // No! Class can't extend multiple classes
class Zoom implements Fi, Baz {}    // OK
interface Vroom extends Fi, Baz {}  // OK
class Yow extends Foo implements Fi {}    // OK


Burn these in, and watch for abuses in the questions you get on the exam.  Regardless of what the question appears to be testing, the real problem might be the class or interface declaration.  Before you get caught up in, say, tracing a complex threading flow, check to see if the code will even compile.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Thursday, August 26, 2010

SCJP Exam Watch 8 - Obfuscate

The exam creators will tell you that they're forced to jam tons of code into little spaces "because of the exam engine." While that's partially true, they ALSO like to obfuscate.

The following code:

Animal a = new Dog();
Dog d = (Dog)a;
d.doDogStuff();

Can be replaced with this easy-to-read bit of fun:

Animal a = new Dog();
((Dog)a).doDogStuff();

In this case the compiler needs all of those parentheses, otherwise it thinks it's been handed an incomplete statement.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Tuesday, May 18, 2010

SCJP Exam Watch 7 - Object Orientation (3)

Don't be fooled by a method that's overloaded but not overridden by a subclass. It's perfectly legal to do the following:

public class Foo {
    void doStuff() {}
}

class Bar extends Foo {
    void doStuff(String s) {}
}

The Bar class has two doStuff() methods: the no-arg version it inherits from Foo (and does not override), and the overloaded doStuff(String s) defined in the Bar class. Code with a refrence to a Foo can invoke only the no-arg version. but code with a reference to a Bar can invoke either of the overloaded versions.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 6 - Object Orientation (2)

Be careful to recognize when a method is overloaded rather than overridden. You might see a method that appears to be violating a rule for overriding, but that is actually a legal overload, as follows:

public class Foo {
    public void doStuff(int y, String s) {}
    public void moreThings(int x) {}
}

class Bar extends Foo {
    public void doStuff(int y, long s) throws IOException {}
}

It's tempting to see the IOException as the problem, because the overridden doStuff() method doesn't declare an exception, and IOException is checked by the compiler.

But the doStuff() method is not overridden! Subclass Bar overloads the doStuff() method, by varying the argument list, so the IOException is fine.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 5 - Object Orientation (1)

If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you're calling the supertype version of the method.

If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception.

Let's take a look at an example:

class Animal {
    public void eat() throws Exception {
        // throws an Exception
    }
}

class Dog extends Animal {
    public void eat() {
        /*no Exceptions*/
    }

    public static void main(String[] args) {
        Animal a = new Dog();
        Dog d = new Dog();
        d.eat();        // OK
        a.eat();        // Compiler error - unreported exception
    }
}

This code will not compile because of the exception declared on the Animal eat() method. This happens even though, at runtime, the eat() method used would be the Dog version, which does not declare the exception.

Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Friday, May 14, 2010

SCJP Exam Watch 4 - Encapsulation

Look out for code that appears to be asking about the behavior of a method, when the problem is actually a lack of encapsulation. Look at the following example, and see if you can figure out what's going on:

class Foo {
    public int left = 9;
    public int right = 3;

    public void setLeft(int leftNum) {
        left = leftNum;
        right = leftNum/3;
    }

    // lots of complex test code here
}

Now consider this question: Is the value of right always going to be one-third the value of left? It looks like it will, until you realize that users of the Foo class don't need to use the setLeft() method! Thay can simply go straight to the instance variables and change them to any arbitrary int value.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 3 - Array Declaration

It is never legal to include the size of the array in your declaration. Yes, we know you can do that in some other language, which is why you might see a question or two that include code similar to the following:

int[5] scores;

The preceding code won't compile. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 2 - Interface Definition

Look for interface difinitions that define constants, but without explicitly using the required modifiers. For example, the following are all identical:

// Looks non-static and non-final, but isn't!
public int x = 1;
// Looks default, non-static, non-final, but isn't!
int x = 1;
// Does't show public or final
static int x = 1;
// Doesn't show public or static
final int x = 1;
// Doesn't show final
public static int x = 1;
// Doesn't show static
public final int x = 1;
// Doesn't show public 
static final int x = 1;
// What you get implicitly
public static final int x = 1;

Any combination of the required (but implicit) modifiers is legal, as is using no moddifiers at all! On the exam, you can expect to see questions you won't be able to answer correctly unless you know, for example, that an interface variable is final and can never be given a value by implementing (or any other) class.



Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 1 - Identifier

The objective says you have to know legal identifiers only for variable names, but the rules are the same for ALL Java components. So remember that a legal identifier for a variable is also a legal identifier for a method or a class. However, you need to distinguish between legal identifiers and naming conventions, such as the JavaBeans standards, that indicate how a Java component should be names. In other words, you must be able to recognize that an identifier is legal even if it does't confirm to naming standards.

If the exam question is asking about naming conventions - not just whether an identifier will compile - JavaBeans will be mentioned explicitly.



Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.