Scope of "private" field

Does anyone know what is the scope of the name of a "private" field in a Java class definition?

It's going to have to be the whole class because you can't declare a variable in a method as private (I think).

Similar Messages

  • Accessing private field of Derived object in Base class

    Hi,
    I have this piece of code I wrote a while ago to test something. The issue is accessing a private field of Base class in Base but of a Derived object.
    Here is the code:
    class Base
         private int x;
         public int getX()
              return x;
         public int getX(Derived d)
              // return d.x;
              return ((Base) d).x;
    }The commented code does not work but casting d to Base does.
    Can someone please explain the reasoning for this.
    Forgot to mention that the compilation error is that x has private access in Base.
    Thank you.
    Edited by: 953012 on Apr 1, 2013 8:42 AM

    >
    As I understand the explanation says that you can access any private member within the code of the class that encloses the private member. So in this case x is the private member and the line of code (return d.x) is in Base which encloses the private member. Does it have to do with the fact that the Derived class does not in fact inherit the private members of Base?
    >
    It has to do with the entire quote from the spec
    >
    A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses
    >
    Your code is
    public int getX(Derived d)
              // return d.x;
              return ((Base) d).x;
         }The 'Derived' class is NOT 'the top level class that encloses the declaration of the member'. It does NOT inherit 'x' which is a private member of 'Base'. As far as the 'Derived' class is concerned 'x' does not exist.
    >
    If outside Base code I have Derived d = new Derived() and I call d.getX() then isn't that like calling d.x in myX()?
    >
    How is that the same? 'Base' owns 'x' and can do whatever it wants with it. 'Derived' has no knowledge of 'x' and CAN NOT access it.

  • ML84 - no scope of list field

    Hi Experts,
    There is no scope of list field in the ML84. I would like to have this list in ALV format.
    How can I do this please?
    Thanks and regards.

    Hi,
    You must activate it via parameter ID ME_USE_GRID = 'X' in the user master record.  
    Regards,
    Edit

  • Private field in a stateless session EJB

    Hi guys,
    I have a question about the design of a stateless session EJB.
    I would like to know :
    Does it make sense to put a private field in a stateless session bean???
    Imagine the following sitiation:
    I have a stateless session bean.
    Its job is to delegate the clients calls to some services (Java objects).
    These services are held in the private field and instatiated when WebLogic invokes
    the ejbCreate method.
    So I would like these services (Java objects) be instantiated before the clients
    invoke the bean's method for delegation - I mean, when WebLogic
    decides to create beans and put it in the pool (on the server startup for example).
    But what happens when the same bean once having finished its job is used by a
    different client???
    Are the services once instatiated available to the new client or WebLogic invokes
    the ejbCreate() method and the services are instatiated one more time???(which
    would be a performence killer)
    I must mention that in order to make the instatiation of the services possible,
    I presume I must implement the ejbCreate() method on the bean implementation class
    which invokes a private method doing the instatiation.
    So, do you think these design is a good design and what is its impact on the performence
    of the stateless session EJB ???
    Thank you.
    Freddy

    Rob Woollen <[email protected]> wrote:
    Freddy wrote:
    Hi guys,
    I have a question about the design of a stateless session EJB.
    I would like to know :
    Does it make sense to put a private field in a stateless session bean???
    Sure
    Imagine the following sitiation:
    I have a stateless session bean.
    Its job is to delegate the clients calls to some services (Java objects).
    These services are held in the private field and instatiated when WebLogicinvokes
    the ejbCreate method.
    So I would like these services (Java objects) be instantiated beforethe clients
    invoke the bean's method for delegation - I mean, when WebLogic
    decides to create beans and put it in the pool (on the server startupfor example).
    But what happens when the same bean once having finished its job isused by a
    different client???
    Are the services once instatiated available to the new client or WebLogicinvokes
    the ejbCreate() method and the services are instatiated one more time???(which
    would be a performence killer)
    WLS keeps the bean instances around in a pool. See
    http://edocs.bea.com/wls/docs81/ejb/session.html#1118700
    I must mention that in order to make the instatiation of the servicespossible,
    I presume I must implement the ejbCreate() method on the bean implementationclass
    which invokes a private method doing the instatiation.
    So, do you think these design is a good design and what is its impacton the performence
    of the stateless session EJB ???It's fine and a pretty common pattern.
    -- Rob
    Thank you.
    Freddy
    Thank you very much Rob.
    Freddy

  • How to discover private field using reflect?

    Hi, all:
    i learned discover fields of a class by using reflect
    getClass().getFields()
    but it only works for public fields. how to discover all
    fields of a class?
    thank you in advance.
    kevin.

    I would not think that you would be able to use reflection to obtain private fields as even if you could get them, you cannot access them outside of the intansiated object of that type.
    what do you want them for?

  • How to get private fields from super class?

    Hi.
    I must get protected and private fields from a class. I know that sounds werid but I have a very good reason for doing so, ask if you want.
    I have tried the getDeclaredField(String) method, but it apparently doesn't return the fields declared by the super classes.
    What's the smartest solution to this?
    Thank you all.
    edit: note that the superclass hierarchy's length is 3 and that there are several classes at the bottom level.
    Edited by: bestam on Sep 24, 2009 2:05 PM

    bestam wrote:
    I do not claim I have invented a new programming language Sir, you must be mistaken. This is not turing complete.
    This is a language for describing Cards or a game's rules if you want.
    AspectJ isn't Turing complete but AspectJ is still a compiler.
    This is how I have been working :
    - I have implemented the core library in Java (what is a Player, what is an Effect, what is a Card, what is a BuildingCard, what is a Player's Turn and so on)
    - It also includes packages dedicated to service, able to retrieve and send data to the clients via sockets.
    - Then I have "hardcoded" a dozen of specific cards in Java, for testing and validating the core library. I have been doing so by extending the BuildinCard's class for example.
    - But my ultimate goal is not to code thoses 1.000+ cards of the game in Java. I chosed to design a little language so that I would end up writing cards faster. While I'm traversing the syntactical tree representing the card, I feed the card's fields one by one. Some of them are quite primitive, some other are more complex and have a recursive nature for instance.
    Providing detail for how you implemented it doesn't change anything about what I already said.
    Thus, this is not really a compiler as it doesn't transform a text in language A into a text in language B.
    You really need to understand more about what "compilers" and certainly compiler theory do before you decide what they can and cannot do.
    And your statement still does not change what I said.
    Is this wrose than the bean design pattern from JSP ? I'm not sure.
    Bean design? A "bean" has almost zero requirements.
    Aside of that, it's a bit harsh to be told "read the fucking manual" while I have written my first compiler some years ago.Not sure who that was directed. I suggested some reading material on compiler theory.
    If you think that your idea is ideal then knock yourself out. Since I doubt I will end up seeing it in anything that I must maintain it doesn't matter to me. But you did in fact ask what the best solution was.

  • Can JVM security policy be modified to access field values of private field

    Can JVM security policy be modified to access field values of "private" field in a Class?

    See http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html#ReflectPermission.
    Note that you will have to use reflection to access the fields once the security policy allows you to supress the access controls.
    Chuck

  • Accessing private fields from an subclass whoes parent is abstract.

    I seem not to be able to access some private fields from a subclass who's parent is abstract...
    here is some code to demonstrate this: (based on yawmark's code)
    import java.lang.reflect.*;
    import java.net.URLClassLoader;
    import java.io.*;
    import java.net.*;
    class PrivateReflection {
        public static void main(String[] args) throws Exception {
            Private p = new Private();
            File f=new File("test.jar");
            URL[] urls=new URL[1];
            urls[0]=f.toURL();
            URLClassLoader loader=new URLClassLoader(urls);
            Class c=loader.getClass();
    //        Class c=p.getClass();
            Field field = c.getField("packages");
            field.setAccessible(true);
            System.out.println(field.get(p));
    class Private {
        private String packages = "Can't get to me!";
        private void privateMethod() {
            System.out.println("The password is swordfish");
    }There is no need for a test.jar file to be created.
    I know for a fact that the ClassLoader abstract class defines a field called "packages" which is an HashMap.
    How do i access this private field?
    I am in the process of making a utility which re-compresses a inputed JAR using other compression methods than normal zip compression while still keeping the same functionality as the inputed JAR.
    I do have it working for Executable JARS: www.geocities.com/budgetanime/bJAR.html
    This old version only works for executable JARs and uses Bzip2 compression.
    I believe i have been able to solve all but one last problem to making re-compressed JARs which work as "library" JARs. This last problem is "removing" temporary prototype classes from the system loader and replacing them with the actual decompressed classes. As there seems not to be an nice way to do this i will have to manually remove references of the classes from the system class loader which is why i am asking this question.

    lol! i have solved my problem... it was because i was using the getField() method instead of the getDeclaredField() method.

  • Private Fields on me card not respected with iCloud sync

    I have set certain fields on my Me card as private (not to be shared.) When my me card is synced to icloud and back down to my iPhone my iPhone does not respect the private fields when sharing my Me card from that device. The fields are respected when shared from my laptop.
    I've been sharing private data with my customers that I expected to remain private. How can I fix this?

    Yes, clearly post iCloud, QuickSync cannot 'see' the contacts list.
    However, here's my question: is it possible to put a duplicate set of contacts where they used to be stored on the local Mac prior to iCloud? Then QS would see them and import them?
    They could always be deleted afterwards if they caused a duplicate set to appear in Address Book for any reason.
    Obviously this is a bit of a kludge as it won't get updated as you add/delete from Address Book. But I'd settle for my 500 contacts being at least transfererd! I could put up with repeatign this workaroudn occasionally.
    Thoughts?

  • Private fields and reflection

    HI!
    Is there a possibillity to acces private members of a class with reflection?
    I get the Information by using
    Class myClass;
    Field[] fiels = myClass.getDeclaredFields();When I try to set of get the value of private field, an access exeption is thrown.
    Any workaround to access this values with reflection (or JNI) ??
    thanks for any hint
    MK

    Thank you for this hint! That's exactlly the method i need. Should found it by myself (sorry)
    Of course this is against OO design principles, but sometimes it is absolutly neccessary to analyze classes.
    Finally I am allready familiar with Java language and I know the meaning of private.......
    mfg
    MK

  • To find a scope of selection field in me84 Tcode

    can any body tell how can i find the table for scope of selection field in me84 Tcode. i tried by using F1 n F9 but it's showing some structure FLABCONFIG. i tried further also but im not getting. actually i have control schedule agreement print program by the value here, for that i need this field. can any body clarify that.
    thanks in advance

    These values are coming from the domain SULPA. These are fixed values.
    YOu have to use the FM: RM_DOMAIN_VALUES_GET
    Usage:
      call function 'RM_DOMAIN_VALUES_GET'
           exporting
                i_name          = 'SULPA'
                i_langu         = sy-langu
                i_read_texts    = 'X'
           importing
                e_domain_values = lt_domain_values.
    Regards,
    Ravi
    Message was edited by:
            Ravi Kanth Talagana

  • Setting private fields from Parent class.

    Hi all, I have what seems to be a weird situation to me.
    Basically I have two classes:
    import java.lang.reflect.Field;
    public class Parent {
         protected void ensureDefaults() {
              Field[] declaredFields = getClass().getDeclaredFields();
              for (Field field : declaredFields) {
                   Object fieldValue = getDefaultValueForType(field.getType());
                   try {
                        System.out.println("defaulting field - name: " + field.getName() + " | this: " + this);
                        field.set(this, fieldValue);
                   } catch (Exception e) {
                        e.printStackTrace();
         private Object getDefaultValueForType(Class<?> type) {
              Object defaultValue = null;
              if (type.isAssignableFrom(String.class)) {
                   defaultValue = "default";
              } else if (type.isAssignableFrom(int.class)) {
                   defaultValue = -100;
              return defaultValue;
    public class Child extends Parent {
         private String name;
         private int age;
         public Child() {
              ensureDefaults();
         public String getName() {
              return name;
         public void setName(String name) {
              this.name = name;
         public int getAge() {
              return age;
         public void setAge(int age) {
              this.age = age;
    // Test Case
    import junit.framework.TestCase;
    public class ChildTest extends TestCase {
         public void testEnsureDefaults() {
              Child child = new Child();
              assertEquals("default", child.getName());
              assertEquals(-100, child.getAge());
    }The odd thing to me is that the output looks like:
    defaulting field - name: name | this: Child@7431b9
    java.lang.IllegalAccessException: Class Parent can not access a member of class Child with modifiers "private"
    ... more exception ...
    defaulting field - name: age | this: Child@7431b9
    java.lang.IllegalAccessException: Class Parent can not access a member of class Child with modifiers "private"
    ... more exception ...
    As you can see, it doesn't like me setting (or getting for that matter - tried that) these fields because they're private. However, if you look it's saying that "this" is a Child, so shouldn't those fields be accessible? Shouldn't ensureDefaults be executed as if it was being called by the Child instance?
    Obviously, I can try to use the accessor methods, but that means creating strings for method names, and then looking for the methods. I'd like to avoid this and it seems to me this should work, no?
    Another odd thing is that if I change the fields in Child to protected, it works fine.
    Also, I'm not sure if this is important (I don't know enough about security managers to know if they're different platform to platform, version to version), I'm on a Mac OSX 10.4.11 and:
    java version "1.5.0_13"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05-241)
    Java HotSpot(TM) Client VM (build 1.5.0_13-121, mixed mode, sharing)
    Any help with this would be greatly appreciated.
    Thanks,
    Eric

    jschell wrote:
    As you can see, it doesn't like me setting (or getting for that matter - tried that) these fields because they're private.Myself I don't like it because it suggests a design problem which is associated with understanding that although a child is a parent that doesn't mean that a parent is a child.
    I understand that, but I don't see how this actually breaks that. The Child is executing a method that is passed down to it from it's Parent, but it's executing it as itself - by that I mean it's not looking at anything that it can't already look at, or at least I thought it was.
    Shouldn't ensureDefaults be executed as if it was being called by the Child instance?No.Ok, I thought it was. Can you please explain this a bit more, I want to understand it.
    >
    Another odd thing is that if I change the fields in Child to protected, it works fine.If you messed with reflection some more you could get to to work even with private. How exactly? I really don't want to bypass any security measures (by settings accessible or using a different security manager, or anything like that). As I mentioned in my last post, what I want to do really is nothing more than a nice way to have a generic toString or hashCode method, if it's not possible to do it nicely - within java's default constraints, I'd rather not.
    >
    However in general the idiom would still be wrong.I'm moving more towards using beans anyway, so I plan on just calling accessor methods which corrects the "wrong idiom" right?
    Thanks for all the help,
    Eric

  • Are private fields public in a static class?

    If a variable in a static class is declared as private, it still seems to be visible. Are access specifiers without any importance in a static class?

    Roxxor wrote:
    tschodt wrote:
    The outer class can access all the fields of the nested class, yes.Yes, but only if the nested class is static, otherwise not.
    This is what I have noticed:
    If the nested class is not static, then the outer class has to instantiate the nested class to access its variables. Well, not really. If the nested class is not static (i.e., it's a nested class), then its variables don't even exist until an instance is created.
    If the nested class is not static, then the nested class can access the outer class´ variables without instantiate it. The inner class can only exist if there exists an instance of the outer class. An inner class (a non-static nested class) contains a reference to the wrapping class. So, it doesn't have to instantiate the outer class, because an instance already exists.
    If the nested class is static, then the nested class has to instantiate the outer class to access its variables. I guess. It seems like a really strange design though.

  • Add a private field in Meta

    Is it possible to add a field in the Metadata that won't export with the image? I want to add a field to show me how much $$ an image has made but don't want that info public, in addition I'd like a field to show me which agencies have the image, also private to me.
    I can't find it in LR, Aperture has this ability but that's one of a very few advantages it has!
    Thanks.
    Patrick

    I'm considering using an empty field for the time being but don't want to add it to one field without having the ability of moving it, en masse, from that field to a newly added field when it becomes available to do so. I'm talking about maybe 1,500 images with sales numbers in them so I need to know I can transfer those different sales amounts to a new field without doing it one at a time, copy/paste.
    Possible current categories would be "Other Categories", "Provider", or "Source".
    I have been using Hindsight's Stockview for years and it has this field in it but LR has become my one stop shop for all image work and just want to use it exclusively, including tracking sales. Maybe it's a bit pre mature.

  • Field: HowTo for private fields.

    Hi;
    Is there a way to get Field to reflect on private properties (no pun intended).
    If declared public, no problem. But if private it throws no such field exception.
    Thanks
    -nat

    In case any of you care why I needed it, please see the following thread message#9.
    http://forum.java.sun.com/thread.jsp?forum=31&thread=479517&start=0&range=15#2234020
    And your input is most appreciated, especially criticism. Rather catch my bugs here....
    Thanks
    -nat

Maybe you are looking for

  • How can i get my icloud restored back to a previous date

    Does anyone know how I can get my icloud restored back to a week ago?  My two separate apple iPhones had their iCloud accounts merged and I got my personal accounts and work accounts merged.  When trying to split them back up again, I deleted all my

  • ABAP Programming to display more than 255 characters in the screen

    Hi    I want to display more than 255 characters in the screen after executing a report but i cant able to do that. Please help me to find out the solution. Thanks   Mrutyunjaya Trpathy

  • Answer FaceTime communication with End User Accepting the call

    Hi, Here is the case, I need the iPad (FaceTime or something else) that will permit me to call an elderly person and talk to this person without having the reciever (the elderly person) of the call do anything. Calling this person and that the call i

  • Photoshop CS3 10.0.1 crashes on "save as"

    Win XP service pak 2 Lots of ram and hdd MasterSuite3 Been running for at least a year with no issues. Today I was running Dreamweaver & Flash & Photoshop enough to thin my resources - ram PS3 may have crashed during the day. After working on many PS

  • Network timeouts

    my alert log randomly shows network issues.. between the primary and secondary dataguard systems.. PRIMARY DATABASE ALERT LOG: Mon Feb 06 09:14:20 2012 Thread 1 advanced to log sequence 4604 (LGWR switch) Current log# 4 seq# 4604 mem# 0: +REDO/osow/o