SCJP 6.0 code snippets – Part 1
Posted by ski | Filed under Developement
Since I started to study to SCJP, I’ll put here some notes. I learn reading SCJP Sun Certified Programmer for Java 6, and making some small code snippets, I’ll paste them + notes here, maybe you’ll find it usefull. I think everyone who learns to SCJP should do his/her own, but if you don’t have time, or want to come back to topic, or something else, you can tak a look on those.
Moreover, please note that
- all uncommented code is COMPILABLE
- parts commented usually do not compile, reason is explained in a comment.
- “dupa” means “ass” in polish ( and it’s used a lot like “foo” and “bar” :) )
- Each section is separate java file
- Names are a bit strange, but it’s like this to keep orderd inside package
Ready – set – go
basics, identifiers
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public class S001Identifiers {
/**
* @param args
*/
public static void main(String[] args) {
int _a;
int $c;
int ______2_w;
int _$;
int €;
int £;
/** NOT LEGAL */
// int :b;
// int -d;
// int e#;
// int .f;
// int 7g
/**
* most weird keywords:
* strictfp assert (1.4) enum (1.5) native goto default
*/
}
class JavaBeanClass {
// public void setMyValue(int v);
// public int getMyValue();
// public boolean isMyStatus();
// public void addMyListener(MyListener m);
// public void removeMyListener(MyListener m);
//
// /** NOT VALID!!!*/
// void setCustomerName(String s) // must be public
// public void modifyMyValue(int v) // can't use 'modify'
// public void addXListener(MyListener m) // listener type mismatch
}
//
//
// ,
}
package must be over import
import between package and class
default class access == “package” access
class access
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public class S002ClassAccess {
}
// default access
class Ass {
}
class A {
};
final class B {
}
// private class C {} // wrong ( outside of the public class)
// protected class D {} // wrogn!!
strictfp final class E {
}; // IEEE 754 rules of floating point
// abstract final class E {}; // wrong!
class NotAbstract {
// abstract method dupa(); // wrong!
}
abstract class CarAbstract {
final void eatThis() {
};
private final void eatThis4() {
}; // only warning!
public final void eatThis2() {
};
}
bounce
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
import java.io.Serializable;
public interface S003Bounceable {
public static int MAX = 100;
// public static volatile int MAX2 = 100; // wrong! wrong!
public static final int MAX3 = 100;
void bounce();
// private void bouncePrivate(); // wrong
// protected void bouncePrivateProt(); // wrong
public void bouncePrivatePub();
abstract void dupa();
// static void eatCock(); // wrong and bad name
}
interface Two extends S003Bounceable, Serializable {
}
class Dupa implements S003Bounceable {
@Override
public void bounce() {
// MAX3 = 3; // WRONG
// MAX = 100;
}
@Override
public void bouncePrivatePub() {
// TODO Auto-generated method stub
}
@Override
public void dupa() {
// TODO Auto-generated method stub
}
}
member modifiers
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public class S004MemberModifiers {
private int a = 0;
public int b = 1;
protected void dupa() {
System.out.println("DUPA = " + a + "," + b);
a++;
}
public static void main(String[] args) {
Ext e = new Ext();
e.doSth();
}
}
class Ext extends S004MemberModifiers {
public void doSth() {
dupa();
}
}
default member access
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public class S005DefaultMemberAccess {
public void testPublic() {
};
protected void testProtect() {
};
private void testPriv() {
};
void testDefault() {
};
}
class Extension extends S005DefaultMemberAccess {
@Override
void testDefault() {
// TODO Auto-generated method stub
super.testDefault();
}
@Override
protected void testProtect() {
// TODO Auto-generated method stub
super.testProtect();
}
protected int testPriv() {
return 0;
} // OK!
}
# def member access – same pkg
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public class S005DefaultMemberAccessSamePkg {
public static void main(String[] args) {
S005DefaultMemberAccess dm = new S005DefaultMemberAccess();
dm.testDefault();
dm.testProtect();
dm.testPublic();
System.out.println("ALL ok");
}
}
other modifiers
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public strictfp class S006OOtherModifiers {
native void methodInC(); // like abstract
// synchronized abstract void syncMe(); // not allowed with abstract!
synchronized void syncMe(){ } ;;;; // mark no implementation
strictfp long method(){ return 1L;};
public void dupa( int dupa, String...strings ){
}
}
local variable init
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public class S007LocalVariables {
int a;
// final int af; // do not allow default value for final
final int af2 = 0;
public void doMe() {
int b; // not initialized (only warning) !!!!
// af2 = 3; // cannot reassign WRONG!!
System.out.println("read a = " + a);
// System.out.println( "read a = " + b); // wrong !
}
public static void main(String[] args) {
S007LocalVariables s = new S007LocalVariables();
s.doMe();
}
}
enum with a variable assigned
/**
* mgorski.pl, This code is published under Creative Commons licence.
* @author Marcin Górski , 2009.
**/
package scjp;
public enum S009Enum {
BIG(10), SMALL(5) {
@Override
public String getCode() { // this overrides code for SMALL
return "B";
}
};
S009Enum(int size) {
this.size = size;
}
private int size;
public int getSize() {
return size;
}
public String getCode() {
return "A";
}
}
Tags: 6, code snippets, Java5, Java6, mock exam, SCJP, study notes






