My new thing learnt for today - Java related

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Currently studying for my SCJP exam, the material they provide you with to study is quite clearly rubbish as it covers no where near enough detail about the nuances of Java. For example

Code:
interface Jumpable {
  int height = 5;
  void jump();
}
class Jumper implements Jumpable {
  public Jumper(int height) {
     height = 10;
  }
  protected void jump() {}
}

Spot the errors in the above code, a little test for you

But anyway that's not what I learnt today, given the following code

Code:
Bob bob1 = new Bob();
bob1.setHeight(10);
System.out.println("bob1 height: " + bob1.getHeight());
Bob bob2 = bob1;
bob2.setHeight(20);
System.out.println("bob1 height: " + bob1.getHeight());

Produces the output:

bob1 height: 10
bob1 height: 20

What does the following code output

Code:
String str1 = "Hello";
System.out.println(str1);
String str2 = str1;
str2 += " World";
System.out.println(str1);

You lot probably already know this, but scroll down to see the answer





















Hello
Hello

Despite the fact that the str2 reference variable refers to the same object, when you make a change to the reference variable the JVM refers it to a different string object so the other string isn't changed!
 
Last edited:
Put you out of your missery

the integer in the interface is implicitly 'public static final', so you can't change the value i.e. height = 10
 
Wouldn't the height = 10 there be changing the value of the parameter to the method, rather than attempting to change the one in the interface, which would be this.height?

nope, as Jumper implements Jumpable, Jumper IS-A Jumpable, and seeing as interface constants are implicitly public final static you can just refer to height. Just did a test to be sure.

This code:

Code:
interface Jumpable {
	int height = 10;
	void jump();
}
public class TestClass implements Jumpable {
  public TestClass() {
  	height = 20;
  }
  public void jump() {}
}

Gives the following compilation error

TestClass.java:7: cannot assign a value to final variable height
height = 20;
^
1 error


EDIT:

Sorry just read what you typed again, yes it should have read

Code:
public Jumper(int height) {
   this.height = height;
}
 
Last edited:
Next question, given the following code in what order does the output appear

Code:
class A {
  { System.out.println("Class A"); }
  public A() { System.out.println("In class A"); }
  static { System.out.println("Static A"); }
}
class B extends A {
  static { System.out.println("Static B"); }
  public B() { System.out.println("In class B"); }
}
public class InitBlocks extends B {
   public static void main(String [] args) {
    new InitBlocks();
    System.out.println("Welcome to the main method");
   }
   static { System.out.println("Static initblocks"); }
}
 
I think it'd be the three statics in the order they're written, then class a, then in class a, in class b and welcome to the main method. I think this because the static bits will be done at first run, then the "class a" will come when class a is loaded by the class loader (caused by the new InitBlocks call and following constructor calls), then the new InitBlocks will call the b constructor which'll call the a constructor to print those.

So you're saying

Static A
Static B
Static initblocks
Class A
In class A
in class B
welcome to the main method

Correct!
 
Back
Top Bottom