Introduction to Expressions,Statements,Blocks
Java Expressions
A Java expression consists of variables, operators, literals, and method calls.
Ex1:
int score; score = 90;
Here, score = 90 is an expression that returns an int.
Ex2:
double a = 2.2, b = 3.4, result; result = a + b - 3.4;
Here, a + b - 3.4 is an expression.
Ex3:
if (number1 == number2)
System.out.println("Number 1 is larger than number 2");
Here, number1 == number2 is an expression that returns a boolean value. Similarly, "Number 1 is larger than number 2" is a string expression.
Java Statements
In Java, each statement is a complete unit of execution.
Ex:
int score = 9*5;
Here, we have a statement. The complete execution of this statement involves multiplying integers 9 and 5 and then assigning the result to the variable score.
In the above statement, we have an expression 9 * 5. In Java, expressions are part of statements.
Java Blocks
A block is a group of statements (zero or more) that is enclosed in curly braces { }.
Example: Program to illustrate Static Methods,Blocks ,Variables
class StaticDemo
{
static int num=10;
static void display()
{
System.out.println("Static Method");
}
static
{
System.out.println("Static Block");
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
display();
}
}
{
static int num=10;
static void display()
{
System.out.println("Static Method");
}
static
{
System.out.println("Static Block");
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
display();
}
}
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac StaticDemo.java
ubuntu@ubuntu:~/Desktop/rkjava$ java StaticDemo
Static Block
Value of num: 10
Static Method
ubuntu@ubuntu:~/Desktop/rkjava$ java StaticDemo
Static Block
Value of num: 10
Static Method
Static Methods:
Static members are those which belongs to the class and you can access these members without instantiating the class. Static keyword can be used with class, variable, method and block. Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without object.
Ex:
class SimpleStaticExample{
// This is a static method
static void myMethod()
{
System.out.println("myMethod");
}
public static void main(String[] args)
{
/* You can see that we are calling this
* method without creating any object.
*/
{
System.out.println("myMethod");
}
public static void main(String[] args)
{
/* You can see that we are calling this
* method without creating any object.
*/
myMethod();
}
}
Output:
myMethod
}
}
Output:
myMethod
Static Block:
Static block is used for initializing the static variables.This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
Ex:
class JavaExample {
static int num;
static String mystr;
static
{
num = 97;
mystr = "Static keyword in Java";
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
num = 97;
mystr = "Static keyword in Java";
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
System.out.println("Value of mystr: "+mystr);
}
}
Output:
Value of num: 97
Value of mystr: Static keyword in Java
}
}
Output:
Value of num: 97
Value of mystr: Static keyword in Java
Example 2: Multiple Static blocks
class JavaExample2
{
static int num;
static int num;
static String mystr;
//First Static block
static
//First Static block
static
{
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}
//Second static block
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}
//Second static block
static
{
System.out.println("Static Block 2");
num = 98;
mystr = "Block2";
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
num = 98;
mystr = "Block2";
}
public static void main(String args[])
{
System.out.println("Value of num: "+num);
System.out.println("Value of mystr: "+mystr);
}
}
Output:
Static Block 1
Static Block 2
}
}
Output:
Static Block 1
Static Block 2
Value of num: 98
Value of mystr: Block2
Value of mystr: Block2
Static Variables
A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.
Few Important Points:
Static variables are also known as Class Variables.Unlike non-static variables, such variables can be accessed directly in static and non-static methods.
Ex: Static variables can be accessed directly in Static method
class JavaExample3
{
static int var1;
static int var1;
static String var2;
//This is a Static Method
//This is a Static Method
static void disp()
{
System.out.println("Var1 is: "+var1);
System.out.println("Var1 is: "+var1);
System.out.println("Var2 is: "+var2);
}
public static void main(String args[])
{
disp();
}
}
public static void main(String args[])
{
disp();
}
}
Output:
Var1 is: 0
Var2 is: null
Ex: Static variables are shared among all the instances of class
class JavaExample
{
//Static integer variable
//Static integer variable
static int var1=77;
//non-static string variable
//non-static string variable
String var2;
public static void main(String args[])
{
JavaExample ob1 = new JavaExample();
{
JavaExample ob1 = new JavaExample();
JavaExample ob2 = new JavaExample();
//Assigning the value to static variable using object ob1
//Assigning the value to static variable using object ob1
ob1.var1=88;
ob1.var2="I'm Object1";
/* This will overwrite the value of var1 because var1 has a single
copy shared among both the objects.*/
ob1.var2="I'm Object1";
/* This will overwrite the value of var1 because var1 has a single
copy shared among both the objects.*/
ob2.var1=99;
ob2.var2="I'm Object2";
ob2.var2="I'm Object2";
System.out.println("ob1 integer:"+ob1.var1);
System.out.println("ob1 String:"+ob1.var2);
System.out.println("ob2 integer:"+ob2.var1);
System.out.println("ob2 STring:"+ob2.var2);
}
}
Output:
ob1 integer:99
ob1 String:I'm Object1
}
}
Output:
ob1 integer:99
ob1 String:I'm Object1
ob2 integer:99
ob2 STring:I'm Object2
ob2 STring:I'm Object2
Java Static Methods
Static Methods can access class variables(static variables) without using object(instance) of the class, however
Static Methods can access class variables(static variables) without using object(instance) of the class, however
non-static methods and non-static variables can only be accessed using objects. Static methods can be accessed directly in static and non-static methods.
Syntax:
static return_type method_name();
Ex:
class JavaExample
static return_type method_name();
Ex:
class JavaExample
{
static int i = 10;
static String s = "Javabook";
//This is a static method
public static void main(String args[])
{
System.out.println("i:"+i);
static String s = "Javabook";
//This is a static method
public static void main(String args[])
{
System.out.println("i:"+i);
System.out.println("s:"+s);
}
}
Output:
i:10
s:Javabook
}
}
Output:
i:10
s:Javabook
Ex: Static method accessed directly in static and non-static method
class StatMthd
{
static int i = 100;
static String s = "Oraclebook";
//Static method
{
static int i = 100;
static String s = "Oraclebook";
//Static method
static void display()
{
System.out.println("i value:"+i);
{
System.out.println("i value:"+i);
System.out.println("Given String:"+s);
}
//non-static method
}
//non-static method
void funcn()
{
//Static method called in non-static method
{
//Static method called in non-static method
display();
}
//static method
public static void main(String args[])
{
StatMthd obj = new StatMthd();
//You need to have object to call this non-static method
}
//static method
public static void main(String args[])
{
StatMthd obj = new StatMthd();
//You need to have object to call this non-static method
obj.funcn();
i++;
//Static method called in another static method
i++;
//Static method called in another static method
display();
}
}
Output:
I:\>javac StatMthd.java
}
}
Output:
I:\>javac StatMthd.java
I:\>java StatMthd
i value:100
Given String:Oraclebook
i value:100
Given String:Oraclebook
i value:101
Given String:Oraclebook
Given String:Oraclebook
Important Points
Static Methods can access static variables without any objects, however non-static methods and non- static variables can only be accessed using objects.
Static methods can be accessed directly in static and non-static methods. For example the static public static void main() method can access the other static methods directly. Also a non-static regular method can access static methods directly.
No comments:
Post a Comment