View RSS Feed

penguinCoder

Class and Method Cheat Sheet

Rating: 1 votes, 5.00 average.
by , 10-17-2012 at 10:42 PM (1806 Views)
Declaring a Class
Java Code:
class ClassName{
  ...code...
}

Declaring a Class with Parent
Java Code:
class ClassName extends parentClassName{
  ...code...
}

Declaring a class with Interface
Java Code:
class ClassName implements interface1, interface2, interface3{
   ...code...
}

Declaring a Class with Fields
Java Code:
class ClassName{
  public [type] publicFieldName;
  private [type] privateFieldName;
  private static [type] staticPrivateFieldName;
  ...code...
}

Declaring a Private Class
Java Code:
private Class className{
  ...code...
}

Declaring a Method
Java Code:
public [returnType] methodName(){
  ...code...
}

Declaring a Method that does Not Return Anything
Java Code:
public void methodName(){
  ...code...
}

Declaring a Method with Parameters
Java Code:
public [returnType] methodName( [type] paramVar1, [type] paramVar2){
   ...code...
}

Declaring a Method with an Array in Parameters
Java Code:
public [returnType] methodName( [type][] arrayName){
  ...code...
}

Declaring a Private Method
Java Code:
private [returnType] methodName(){
  ...code...
}

Declaring a Method with varargs
Varargs is a feature that allows an undefined amount of parameters to be passed in, and used like an array. It must be the last item in the parameter list.
Java Code:
public [returnType] methodName( [type]... varName ){
   ...code...
}

Method Signature
Java Code:
methodName( [type] paramVar1, [type] paramVar2 )

Method Overloading
Java is able to distinguish between several methods with the same name based upon what is called in the parameter list. However, it does NOT differentiate different methods based on its return type.
Java Code:
public class ClassName{
  ...fields...

  public void methodName( String s ){
    ...code...
  }
  public void methodName( int i ){
    ...code...
  }
  public void methodName( double d ){
    ...code...
  }
  public void methodName( boolean b ){
    ...code...
  }
  public void methodName( String s, int i){
    ...code...
  }
}

Adding a Constructor to a Class
Java Code:
public class ClassName{
  ...fields...

  public ClassName( [type] paramVar1, [type] paramVar2 ){
    fieldName1 = paramVar1;
    fieldName2 = paramVar2;
  }
}

Adding Multiple Constructors to a Class
Java Code:
public class ClassName{
  ...fields...

  public ClassName(){
    fieldName1 = 0;
    fieldName2 = 0;
  }

  public ClassName( [type] paramVar1, [type] paramVar2 ){
    fieldName1 = paramVar1;
    fieldName2 = paramVar2;
  }
}

Creating a Constructor with Parameters that Shadow Fields
Shadowing fields makes your code difficult to read, and is conventionally only used within Constructors or Methods that set a particular Field.
Java Code:
public class ClassName{
  private [type] fieldName1;
  private [type] fieldName2;

  public ClassName( [type] fieldName1, [type] fieldName2 ){
    this.fieldName1 = fieldName1;
    this.fieldName2 = fieldName2;
  }
}

Calling the Constructor, with Arguments, to Initialize Object
Java Code:
ClassName classObject = new ClassName( [value] , [value] );

Calling the Constructor, with No Arguments, to Initialize Object
Java Code:
ClassName classObject = new ClassName();

Submit "Class and Method Cheat Sheet" to Facebook Submit "Class and Method Cheat Sheet" to Digg Submit "Class and Method Cheat Sheet" to del.icio.us Submit "Class and Method Cheat Sheet" to StumbleUpon Submit "Class and Method Cheat Sheet" to Google

Updated 10-18-2012 at 12:30 AM by penguinCoder

Categories
Cheat Sheet

Comments