This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ajd.examples.basics; | |
public class FinalVariable { | |
final int finalInstanceField | |
// =5 | |
; | |
static final int finalStaticField = 5; | |
{ | |
// finalField = 5; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FinalVariable { | |
// in the instance initializer expression, or while declaration itself | |
// final <type> <variable_name> = <initializer expression>; | |
final int finalInstanceField = 5; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FinalVariable { | |
final int finalInstanceField; | |
{ | |
// Initialization in instance initializer block | |
finalInstanceField = 5; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FinalVariable { | |
final int finalInstanceField ; | |
public FinalVariable() { | |
// constructor | |
finalInstanceField = 7; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FinalVariable { | |
// in the instance initializer expression, or while declaration itself | |
// final <type> <variable_name> = <initializer expression>; | |
static final int finalStaticField = 25; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FinalVariable { | |
static final int finalStaticField; | |
static { | |
finalStaticField = 7; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Shape { | |
int length; | |
int width; | |
private Shape(int length, int width) { | |
length = length; | |
width = width; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer