Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged

Sp7 #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 27 Chapter6/SDemo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Use a static method.
class StaticMeth{
static int val = 1024; // a static variable

// a static method
static int valDiv2() {
return val/2;
}

}


public class SDemo2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("val is " + StaticMeth.val);
System.out.println("StaticMeth.valDiv2(): " + StaticMeth.valDiv2());

StaticMeth.val = 4;
System.out.println("val is " + StaticMeth.val);
System.out.println("StaticMeth.valDiv2(): " + StaticMeth.valDiv2());

}

}
31 changes: 31 additions & 0 deletions 31 Chapter6/SDemo3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Use a static block
class StaticBlock{
static double rootOf2;
static double rootOf3;

// This block is executed when the class is loaded.
static {
System.out.println("Inside static block.");
rootOf2 = Math.sqrt(2.0);
rootOf3 = Math.sqrt(3.0);
}

public StaticBlock(String msg) {

System.out.println(msg);

}
}

public class SDemo3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
StaticBlock ob = new StaticBlock("Inside Constructor");

System.out.println("Square root of 2 is " + StaticBlock.rootOf2);
System.out.println("Square root of 3 is " + StaticBlock.rootOf3);

}

}
19 changes: 19 additions & 0 deletions 19 Chapter6/StaticError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

public class StaticError {

int denom = 3; // A normal instance variable
static int val = 1024; // A static variable

/* Error! Can't access a non-static variable from within a static method. */
static int valDivDenom() {
return val/denom; // won't compile!
}

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("StaticError.valDivDenom = " + StaticError.valDivDenom());

}

}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.