Skip to content

Instantly share code, notes, and snippets.

@ajduke
ajduke / FinalVariable.java
Created April 1, 2012 03:31
Initializing Final variables
package ajd.examples.basics;
public class FinalVariable {
final int finalInstanceField
// =5
;
static final int finalStaticField = 5;
{
// finalField = 5;
@ajduke
ajduke / FinalVariable.java
Created May 13, 2012 08:07
Final Instance variable
public class FinalVariable {
// in the instance initializer expression, or while declaration itself
// final <type> <variable_name> = <initializer expression>;
final int finalInstanceField = 5;
}
@ajduke
ajduke / FinalVariable.java
Created May 13, 2012 08:27
Initializing final variable
public class FinalVariable {
final int finalInstanceField;
{
// Initialization in instance initializer block
finalInstanceField = 5;
}
}
@ajduke
ajduke / FinalVariable.java
Created May 13, 2012 08:31
Final variable initialization
public class FinalVariable {
final int finalInstanceField ;
public FinalVariable() {
// constructor
finalInstanceField = 7;
}
}
@ajduke
ajduke / FinalVariable.java
Created May 13, 2012 16:03
Final static Variable
public class FinalVariable {
// in the instance initializer expression, or while declaration itself
// final <type> <variable_name> = <initializer expression>;
static final int finalStaticField = 25;
}
@ajduke
ajduke / FinalVariable.java
Created May 13, 2012 16:31
Final Static Variable
public class FinalVariable {
static final int finalStaticField;
static {
finalStaticField = 7;
}
}
@ajduke
ajduke / ThisOperator1.java
Created August 19, 2012 07:46
This operator when same parameter name
public class Shape {
int length;
int width;
private Shape(int length, int width) {
length = length;
width = width;
}
}
@ajduke
ajduke / ThisOperator2.java
Created August 19, 2012 07:59
Assigning the field name
public class Shape {
int length;
int width;
private Shape(int length, int width) {
// access member variable incase the
// field name and parameter name are same
this.length = length;
this.width = width;
}
}
public class Shape {
int length;
int width;
public void setLength(int length) {
// access the instance variable
this.length = length;
}
public void setWidth(int width) {
// access the instance variable
this.width = width;
public class Shape {
private void printThis() {
// prints invoking object
System.out.println(this);
}
public static void main(String[] args) {
Shape shape = new Shape();
// print the shape reference
System.out.println(shape);
// print the shape reference inside method