-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStaticKeyword.java
More file actions
47 lines (43 loc) · 1.46 KB
/
StaticKeyword.java
File metadata and controls
47 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package miscellaneous;
import java.util.Scanner;
public class StaticKeyword{
static int k = 6;
public static void main(String[] args){
int k = 5;
System.out.println(k);
System.out.println(StaticKeyword.k);
welcome(); // calling static method without instance of the class
Scanner s = new Scanner(System.in);
System.out.println("enter the product id: ");
int id = s.nextInt();
s.nextLine();
System.out.println("enter the product name: ");
String name = s.nextLine();
StaticKeyword sk = new StaticKeyword();
// non static methods called using instance of the class
sk.getProductDetails(id, name);
sk.showDetails();
s.close();
}
int id;
String product;
static String manufacturer; // static data member
// static block
static{
System.out.println("static block: will be executed first");
System.out.println("static members initialized:");
Scanner s = new Scanner(System.in);
manufacturer = s.nextLine();
}
private void getProductDetails(int id, String product){
this.id = id;
this.product = product;
}
public void showDetails(){
System.out.println("\nproduct id: "+id+"\t product name: "+product+"\nmanufacturer: "+manufacturer);
}
// static method
static void welcome(){
System.out.println("welcome to the static keyword demo :)");
}
}