Forward Declaration and "this" keyword

Consider this code:
class A {
   private int i = 2 * this.j;  // This will be calculated as 2 * 0 = 0
//   private int i = 2 * j;  //This code will not compile
   private int j = 20;
}Why the "this" keyword is required for the j to be accessible? Though it is a forward declaration, what is the significance of "this" which gives visibility to the variable j. Please give some light to this.

Though it is a forward declaration, what is the significance of "this" which gives
visibility to the variable jI don't think "this" alters the visibility of j: that is the instance variable j is in scope. However "Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope."
See "8.3.2.3 Restrictions on the use of Fields during Initialization" http://java.sun.com/docs/books/jls/third_edition/html/classes.html#287410
Using "this" you have a reference to the object being constructed with the j instance variable sill having its default value of zero.
Such instance initialisers would appear to be inherently less intelligible than using a constructor.

Similar Messages

  • Package forward declaration

    Can anyone kindly explain me in detail about Package forward declaration and where this is used and what's the purpose of using it.A example would be good
    Thanks in advance

    A subprogram declaration called a
    forward declaration. It consists of the subprogram
    specification in the package body .
    CREATE OR REPLACE PACKAGE BODY declare_forward
    IS
    PROCEDURE pro(. . .);      -- forward declaration
    PROCEDURE pro2(. . .)
    IS                         -- subprograms defined
    BEGIN           -- in alphabetical order
    pro(. . .);
    END;
    END declare_forward;

  • This and super keywords, array.length attribute

    'this' and 'super' keywords and array.length attribute are declared in which java class?
    and also during running of a java program how they are initialized and how they work?

    'this' and 'super' keywords and array.length attribute are declared in which java class?They are not declared in any Java class, any more than the keywords 'class', 'interface', 'for', 'while', etc., are. They are defined in the grammar of the Java language, in the Java Language Specification.
    and also during running of a java program how they are initialized and how they work?That's much too large a question for a forum. Try reading the Java Tutorial.

  • Email messages from my personal email account are being posted (copied) to SMS messages.  I have a new cell device, and this is happening on the new device also.  The copy/forward to SMS is NOT being initiated by my email provider.  How do I end this?

    Email messages from my personal email account are being posted (copied) to SMS messages.  I have a new cell device (swtched from Droid to iPhone) and this is happening on the new device also.  The copy/forward to SMS is NOT being initiated by my email provider, and not by Gmail.   How do I end ûVerizon's forwarding my personal email to SMS? 
    Thanks.

    Maybe I'm missing something but how can Verizon have control over your personal GMail account?  Sounds like you do have your email set up to forward to sms. 
    I have certain emails that are forwarded to me as SMS but that is an option that is enabled on the email side of things and is independent from Verizon.

  • I have the iPad 2.  In forwarding an email this morning, it froze up, can't access the email or anything else for that matter.  I turned the iPad off and am now trying to turn it back on, but nothing is happening.

    I have the iPad 2.  I was forwarding an email this morning, it froze up.  I tried turning off the iPad and now I can't get it to turn back on?  Any suggestions?  The only thing that is on the black screen is a small circle of moving items.....

    I'm glad it solved your problem

  • This and super keyword

    what is the difference between this and super keyword?

    this serves as a reference to the current object whilst super refers to the superclass of the current object.

  • The back, forward, reload and stop buttons are greyed out, the address bar stays blank when I go to a website and firefox loads to a blank page, not my home page. There are 3 users on this computer and it only happens on one user.

    Firefox loads to a blank page, not my home page. When I click on the home button it takes me to the home page. The back, forward, refresh and stop buttons are greyed out and not usable. The webpage I am on does not show up in the address bar. If I type something into the address bar, that address stays there no matter what page I go to next.

    Just upgraded to v4. It did NOT fix the problem.

  • Using "this" keyword

    Please help me. I didn't find an explanation in manuals or tutorials. Thank you
    //THIS PROGRAM IS WORKING BUT I CANNOT UNDERSTAND IT
    class one{
         public one(){
         System.out.println("This is from constructor");
         //PLEASE MAKE ME UNDERSTAND THIS METHOD IN THE WAY IT IS DECLARED "one two()"!!!!!!!!
         //I'M ALSO CONFUSED ABOUT THE USE OF "THIS" KEYWORD
         one two(){
              System.out.println("This is from method two()");
              return this;
    class From_main{
         public static void main(String[] args){
              one a=new one();
              one b=new one();
              a.two();
              b.two();
         }

    IMHO the trouble with cute trivial Examples like this is that; the names are meaningless and/or tend to confuse
    and they don't represent anything useful that you might do in real life.
    It might help if we renamed the class and methods and added some comments
    class Trivial {
      public Trivial(){
        System.out.println("This is from constructor");
      /** Returns a reference to an instance of this class */
      Trivial getInstance(){
        System.out.println("This is from method getInstance()");
        return this;
    class From_main {
      public static void main(String[] args){
        Trivial a = new Trivial();
        Trivial b = new Trivial();
        Trivial aa = a.getInstance();
        // a and aa point to the same 'Trivial' instance
        if ( a == aa)
          System.out.println("a == aa");
        Trivial bb = b.getInstance();
        // b and bb point to the same 'Trivial' instance
        if ( b == bb)
          System.out.println("b == bb");
    }The method getInstance() is redundant as you have to have a reference to the object
    before you can invoke the method to get a reference to the object.
    There are better ways to illustrate how to use the 'this' keyword.

  • Forward Declaration in Class Builder

    Hi All,
       In the Class Builder (SE24)when i create a new Class, There is an option of Forward declaration  in the Property Tab. In this, we can add Type Group Interface and Class. What is the Purpose of this forward declaration?
      The F1 help does not provide any help.
    Regards,
    Kapil.

    in addition to above replies, it can be used for classes and interfaces in that case Forward declaration is equivalent to
    CLASS <class_name> DEFINITION LOAD.
    interface <interface_name> load
    statements which are normally used in Program ( case for local classes )
    use of Load from SAP docu
    ... LOAD
    Effect
    The variant with the LOAD addition loads a global class class from the Class Library. This statement was needed before Release 6.20 if you wanted to access one of the static components of class from within a program, or to declare an event handler for class before class had been loaded automatically. From Release 6.20 onwards, the LOAD addition is only needed if the compilation of an ABAP program fails because it includes recursive accesses of a globa l class. In such cases, you may be able to make the program compilable by explicitly loading the class before recursion.
    Thanks,
    kranthi.

  • Use of forward declaration

    Hi Experts,
                   Please let me know the use of forward declaration for a class. How to use that?
    Thanks and Regards,
    Debarshi

    Hi,
    Absolutely true matt...
    you use forward declaration when you want to indicate that this component will be defined later but i am referring to this as of now.
    so that it does not give any syntax/ run time error.
    Rgds/Abhi

  • No logical forward declared in action {0} in Web Channel

    Hello
    We are getting this error (No logical forward declared in action ) in CRM Web Channel when we are trying to retrieve a service order.
    Any Ideas????

    Hello,
    I would guess that this maybe due to come config in your struts-config.xml
    I would check the f tansaction type that is been used and the transaction category
    The only transaction category working with ICSS are:                                   
    - BUS2000116                                                                       
    - BUS2000120                                                                       
    - BUS2000112                  
    Regards
    Mark

  • Querstion regarding the "this" keyword

    hi everyone,
    i've doubt regarding the usage of this keyword in the inner classes & static methods. like the following:
    class A {
        int val1 = 0;
        int val2 = 0;
        A() {
            B b = new B();
            C c = new C();
            for(int i=1; i < 11; i++) {
                b.add(i);
                c.add(i);
                System.out.println("Val 1 : " + val1);
                System.out.println("Val 2 : " + val2);
        public static void main(String args[]) {
            new A();
        class B {
            public void add(int val) {
                // the this keyword is used as static variable
                A.this.val1 += val;
        static class C {
            public void add(int val) {
                // the "this" keyword cannot be used
                //A.this.val2 += val;
    }the code doesn't matter what its meant for, i wrote code to explain u my doubt with example.
    so any explanation from ur side, i'm not clear with the this keyword usage.
    thanx

    I am in agreement with trinta, although I think your doubt arises from a lack of understanding of inner classes, not from the use of 'this'. You seem very adept in the use of 'this'.
    Realise that there are 4 categories of nested classes:
    1> Top-level or static nested classes
    These are declared by the static keyword (as in class C in your example). They can be instantiated with or without an instance of the containing class. i.e. using the class names from your example.
    C c = new A.C();
    or
    C c = new instanceOfA.C();
    Since a static nested class does not require an instance of a class, there is no use of the 'this' keyword. There may not be any instance created, if if there was, which one since you don't have to specify which when creating it (see first example code above).
    2> Member Inner Classes
    As in class B in your example. The name should indicate that we think about these classes as members, or part of, the containing instance. Use these classes when it makes no sense for the inner class to exist outside the containing class. Usually these inner classes are designed to be helper classes and often hidden from view. When they aren't, the only way to create new instances of them is through an actual instance of the containing class. i.e. using the class names from your example,
    B b = new instanceOfA.B();
    Note that this code
    B b = new A.B();
    will yield an error. There must be an associated containing class.
    For this reason, the use of 'this' is only appropriate for member classes.
    3> Local Inner Classes
    4> Anonymous Inner Classes
    Not really relevant to this question.

  • Receiving the warning of class forward declaration

    I am trying to build the Clustering Plug in project on my Leopard. I have following 2 queries -
    In that project an interface class is defined as
    @interface ClusteringController : NSWindowController { ....... ..... .... } @end.
    And this class is used in implementation class using forward declaration as follows
    @class ClusteringController;
    then in one fuction it is used as follows
    (long) filterImage:(NSString*) menuName {
    ClusteringController *cluster = [[ClusteringController alloc] init]; [cluster showWindow:self]; return 0; }
    When i try to build this project it showing a warning as follows
    warning: receiver 'ClusteringController' is a forward class and corresponding @interface may not exist
    Also there is 1 more warning is coming
    warning: no '-updateProxyWhenReconnect' method found
    This warning is coming for the following line of code
    if(delegate) [delegate updateProxyWhenReconnect];
    Can anybody help me to overcome these warnings?

    shaktirsg wrote:
    And this class is used in implementation class using forward declaration as follows
    @class ClusteringController;
    An implementation requires an #import of the entire interface file for any class used in the code. As a rule:
    Use @class when a class is used in an @interface
    Use #import when a class is used in an @implementation
    if(delegate) \[delegate updateProxyWhenReconnect\];
    warning: no '- updateProxyWhenReconnect' method found
    It looks like the compiler doesn't know the class of 'delegate'. Can we see the code that sets the 'delegate' variable? Also please let us know where updateProxyWhenReconnect is declared. Is it declared in the interface for the class to which 'delegate' belongs? If so, it might be good for us to also see that @interface file.
    \- Ray

  • On 3G we cannot send emails from either our iPhones or iPad and this also happens with some WiFi connections but not all.  Yet we can always send emails from our Hotmail Email account.  What is causing this and what do we need to do to resolve it?

    On 3G we cannot send  Business emails from either our iPhones or Ipad and this also happens with some WiFi connections but not all.  Yet we can always send emails from our Hotmail Account using both 3G and WiFi.
    We bought the iPhones and Ipad so that we could send emails while we are out of the Office, but we are not able to do this unless we can find a WiFi connection. Incoming emails are fine.  We use IMAP, for Business emails just incase this is relevant and I know that Hotmail is POP3.
    Our technical IT knowledge is not great, so we look forward to your suggestions as to how to resolve this. 

    Contact whomever supports the email account and get the correct Outgoing email server settings from them.

  • I have edited photos in i-photo and would like to have them printed.  How do i burn them to disc in a format that a windows PC system can read.  I am in India and this is definitely "microsoft" country!  thanks for taking the time to guide me.

    I have edited photos in i-photo and would like to have them printed at any photo development lab.  I am in India and this is definitely "microsoft" country !
    Please advise me how to burn these edited photos to a disc to take to the nearest printing store in a format that "windows" system can understand.
    Thanks in advance.  I look forward to learning about this. 

    Select the pics in the viPhoto Window and go File -> Export and export them to a folder on the desktop. Burn that to disk with the Finder's burn command. Result. A disk with a folder of images that any machine can understand.
    Regards
    TD

Maybe you are looking for

  • Traffic lights on reports disappears

    Hi Experts, I have made the traffic lights settings in WAD exceptions and saved that by Personalising it.Now this report is integrated in portal and it working fine with traffic light since its saved as an View,but in the report I have many fields in

  • File on webser

    Hello Guys, I want to put 1 MS word file on our peoplesoft web server and want to provide a link on the page where user can click to download that file to their local machine. Can you help me with the code and tell me where exactly should i put the f

  • Black spot on the center screen

    Hi I have baught blackberry curve 9300 from Bahrain Airport last week.. now my berry shows a black spot on the center in 1.5 Cm lenght its visible when screen is dark but the white screen doesnt shows clearely. I am afraid whether any daamage happend

  • I have an idea (new feature) for Firefox

    Is it possibe to make more than 9 visual bookmarks? It'll be very great!

  • How to delete a Security Zone?

    Hello, I've deployed a certain PAR file with a certain Security Zone: <property name="SecurityZone" value="roy.test.TestApp/DefaultSecurity"> </property> Now I can see this security under System Config -> Permissions -> Security Zones as I should. My