Doubt in the Module3 Exercise | Selenium Forum
M
Posted on 09/12/2015
Program:

public class Test {

Test t1= new Test();
int i;
static int j;
static Test t2 = new Test();
public static void main(String[] args) {
t1.i=10; //1
i=19; //2
j=10; //3
t2.i=19; //4

}
}

what is the line static Test t2 = new Test(); means

Doubt 2:
Program:
public class Test {


public static void main(String[] args) {
public int a; // 1
protected int b; // 2
private int c; // 3
static int d; // 4
transient int e; // 5
volatile int f; // 6
final int g = 1; // 7
int i=7; // 8
int h; //9
System.out.println(h); //10

}
}

what are the protected, public, private all those key words i am not aware of those can you please explain

M
Replied on 09/12/2015

| Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public | y | y | y | y
————————————+———————+—————————+——————————+———————
protected | y | y | y | n
————————————+———————+—————————+——————————+———————
no modifier | y | y | n | n
————————————+———————+—————————+——————————+———————
private | y | n | n | n

y: accessible
n: not accessible



Private
Like you'd think, only the class in which it is declared can see it.

Package Private
Can only be seen and used by the package in which it was declared. This is the default in Java (which some see as a mistake).

Protected
Package Private + can be seen by subclasses or package member.

Public
Everyone can see it.