Answer : t is non-static and it cannot be referenced in a static context in the main method.
Explaination :
Here look for the main(String args[]) method that is static.
Declaration of any method as static means that it will be created only once at time when the class loads.
Also note that non static method is created when object is instantiated.
In given problem, t is non-static and we are trying to fetch from static context i.e, main method
Since t is non-static, memory has not yet assigned to it.
To assign a memory for t you can create object and then use your t as example
public class Test{
private int t;
public static void main(String args[]){
Test test = new Test(); // we are creating object of Test giving some memory allocation to t
System.out.println(test.t);// from object we can use t
}
}
There is another way to use t directly. Make t as static , when Test class loads static t allocated with memory
It will be looking as
public class Test{
private static int t;
public static void main(String args[]){
System.out.println(t);// t has assigned some memory, by default int has 0, so print will be 0.
}
Why all other options are wrong:
1 . Since variable t is private it can be accessed from there subclass. (Given option violates definition itself)
2. Program will give you compile time error as memory is not assigned to t which is being referred by main method.
3. This is the correct answer.
4 . variable t is not initialized but declared as int which is by default initialized to 0. (Error is not caused through this reason)
5. variable x is not initialized which we are not using it. Error is not caused through this reason.
Correct answer is t is non-static and it cannot be referenced in a static context in the main method.
Hope you got my point and must be helpful. Vote up. Thanks.
Analyze the following code: public class Test { private int t; public static void main(String[] args)...
Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } t is non-static and it cannot be referenced in a static context in the main method. The program compiles and runs fine. The variable t is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.
Analyze the following code: class Test ( private int t static int x; public void method() Test test new Test); System.out.println (x); a. The variable t is not initialized and therefore causes er The program compiles fine but has runtime errors because main method. cThe variable t is private and therefore cannot be accessed in t method. The program compiles and runs fine
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
class Test public static void main(String args) { Aa=new AO: a.printo: class A private String s; public A (String news) { 8 = news: public void print { System.out.println(s): The program would compile and run if you change A a new Alto Aa=new A('5'). The program has a compilation error because the instance variables in class A is not public. The program has a compilation error because class A does not have a no-arguments constructor The program compiles and runs...
Analyze the following code: public class Test { public int x; public Test(String t) { System.out.println("Test"); public static void main(String[] args) { Test test: System.out.println(test.x); The program has a compile error because Test class does not have a default constructor The program has a compile error because test is not initialized OO The program has a compile error because x has not been initialized The program has a runtime NullPointerException while executing test.x because test is a null reference and...
how to change this code to string instead of int public static void main(String[] args) { int[] sample = { 212, 580, 6, 7, 28, 84, 112, 434}; Dictionary bst = new Dictionary(); for (int x : sample) { bst.insert(x); } System.out.println(bst.find(65)); System.out.println(bst.smallest()); System.out.println(bst.largest()); // bst.delete(84); System.out.println(bst.numOfLeafNodes()); System.out.println(bst.height()); bst.traverseInOrder(); } }
c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is t1's i = 2 ? It should be...
What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i; System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); } }
What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...
What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);