Author: AlinaIoana Florea
Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/java-static-vs-nonstatic-variables
Question 294 / 297:  (SCJP 6) Find the errors in:
Choose 2
class X 

{

X()

{

System.out.print(a);

static int a; // line 1

a=4;

System.out.print(a);

System.out.print(b);

}

int a;

static int b;

}

public class Test extends X

{

public static void main(String[] args)

{

X x = new X();

System.out.println(a + "" + x.a + X.a); // line 2

System.out.println(b + "" + x.b + X.b);

}

}

<< First < Previous Next > Last >>
Explanation:

Static variables are created only once when the the class is first created/loaded in the memory by JVM, so they are not created every time an object of their type/class is created with the "new" operator. Therefor static variables exist before any object is created, so they can be called directly on the class not through an object.

line 1- declaring an "int a;" in X() it's allowed and since its declaration it would just shadow, only inside X(), the instance variable with the same name.

If the incorrect code is removed the app will print:

040

4000

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Jordon Humphreys
Start Quiz
Yasser Ibrahim
Start Quiz
Joanna Smithback
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>