About Question enthuware.ocajp.i.v8.2.1275 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Hissam
Posts: 3
Joined: Fri Sep 09, 2016 10:57 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1275 :

Post by Hissam »

It clearly says
How can you declare 'i' so that it is not visible outside the package test.

that implies it should be visible inside the package.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1275 :

Post by admin »

I am sorry I do not agree that there is any such implication. All it means and implies that i should not be visible outside the package test. It doesn't say anything about inside the package.

thank you,
Paul.

Hansolo
Posts: 1
Joined: Fri Feb 01, 2019 11:36 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1275 :

Post by Hansolo »

Code: Select all

// Yoda: Do or do not there is no try

package have;

public class TheGoods {

   private String forEyesOnly = "I am supposed to be private";

}

Code: Select all


package havenot;

import java.lang.reflect.Field;
import have.TheGoods;

public class AccessTester {

public static void main(String[] args) {
 
 TheGoods theGoods = new TheGoods();
 
        try {
         
            Field field = TheGoods.class.getDeclaredField("forEyesOnly");
            field.setAccessible(true);
            String answer = (String)field.get(theGoods);
            System.out.println( answer + " but anyone can read it if they really want to.");
            
        } catch (Exception e) {
        }
    }  

}


admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1275 :

Post by admin »

Let's stay within the scope of OCAJP exam. Reflection is not on the exam.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests