Java - this keyword inside new operator












-1















public class FXMLDocumentController implements Initializable {

@Override
public void initialize(URL url, ResourceBundle rb) {

listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
@Override
public void updateItem(String name, boolean empty){
super.updateItem(name,empty);
if (name.equals("Vender")){
setText(name);
((Node)this).setId("id");
}
}
});
}
}


I want that my this keyword whether reference itself to my new ListCell<String> rather than FXMLDocumentController, once ListCell extends



java.lang.Object,
**javafx.scene.Node**,
javafx.scene.Parent,
javafx.scene.layout.Region,
javafx.scene.control.Control,
javafx.scene.control.Labeled,
javafx.scene.control.Cell<T>,
javafx.scene.control.IndexedCell<T>,
javafx.scene.control.ListCell<T>


and class Node have method setId.



How reference my new class inside of implementation?










share|improve this question

























  • Cannot reproduce this error. The only compiler issue in your code seems to be that listViewArtigosMercado is not declared in FXMLDocumentController. BTW: Note that multiple items can be used with the same ListCell. You may end up with cells containing items other than "Vender" unless you add a else clause fixing this. (Same for the text property). Furthermore cells can be empty. In this case item (name) is null which results in a NullPointerException in your code.

    – fabian
    Nov 25 '18 at 11:40


















-1















public class FXMLDocumentController implements Initializable {

@Override
public void initialize(URL url, ResourceBundle rb) {

listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
@Override
public void updateItem(String name, boolean empty){
super.updateItem(name,empty);
if (name.equals("Vender")){
setText(name);
((Node)this).setId("id");
}
}
});
}
}


I want that my this keyword whether reference itself to my new ListCell<String> rather than FXMLDocumentController, once ListCell extends



java.lang.Object,
**javafx.scene.Node**,
javafx.scene.Parent,
javafx.scene.layout.Region,
javafx.scene.control.Control,
javafx.scene.control.Labeled,
javafx.scene.control.Cell<T>,
javafx.scene.control.IndexedCell<T>,
javafx.scene.control.ListCell<T>


and class Node have method setId.



How reference my new class inside of implementation?










share|improve this question

























  • Cannot reproduce this error. The only compiler issue in your code seems to be that listViewArtigosMercado is not declared in FXMLDocumentController. BTW: Note that multiple items can be used with the same ListCell. You may end up with cells containing items other than "Vender" unless you add a else clause fixing this. (Same for the text property). Furthermore cells can be empty. In this case item (name) is null which results in a NullPointerException in your code.

    – fabian
    Nov 25 '18 at 11:40
















-1












-1








-1








public class FXMLDocumentController implements Initializable {

@Override
public void initialize(URL url, ResourceBundle rb) {

listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
@Override
public void updateItem(String name, boolean empty){
super.updateItem(name,empty);
if (name.equals("Vender")){
setText(name);
((Node)this).setId("id");
}
}
});
}
}


I want that my this keyword whether reference itself to my new ListCell<String> rather than FXMLDocumentController, once ListCell extends



java.lang.Object,
**javafx.scene.Node**,
javafx.scene.Parent,
javafx.scene.layout.Region,
javafx.scene.control.Control,
javafx.scene.control.Labeled,
javafx.scene.control.Cell<T>,
javafx.scene.control.IndexedCell<T>,
javafx.scene.control.ListCell<T>


and class Node have method setId.



How reference my new class inside of implementation?










share|improve this question
















public class FXMLDocumentController implements Initializable {

@Override
public void initialize(URL url, ResourceBundle rb) {

listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
@Override
public void updateItem(String name, boolean empty){
super.updateItem(name,empty);
if (name.equals("Vender")){
setText(name);
((Node)this).setId("id");
}
}
});
}
}


I want that my this keyword whether reference itself to my new ListCell<String> rather than FXMLDocumentController, once ListCell extends



java.lang.Object,
**javafx.scene.Node**,
javafx.scene.Parent,
javafx.scene.layout.Region,
javafx.scene.control.Control,
javafx.scene.control.Labeled,
javafx.scene.control.Cell<T>,
javafx.scene.control.IndexedCell<T>,
javafx.scene.control.ListCell<T>


and class Node have method setId.



How reference my new class inside of implementation?







java inheritance javafx this anonymous-class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 15:19









Andrew Tobilko

27.5k104285




27.5k104285










asked Nov 24 '18 at 14:59









Pedro CorreiaPedro Correia

53




53













  • Cannot reproduce this error. The only compiler issue in your code seems to be that listViewArtigosMercado is not declared in FXMLDocumentController. BTW: Note that multiple items can be used with the same ListCell. You may end up with cells containing items other than "Vender" unless you add a else clause fixing this. (Same for the text property). Furthermore cells can be empty. In this case item (name) is null which results in a NullPointerException in your code.

    – fabian
    Nov 25 '18 at 11:40





















  • Cannot reproduce this error. The only compiler issue in your code seems to be that listViewArtigosMercado is not declared in FXMLDocumentController. BTW: Note that multiple items can be used with the same ListCell. You may end up with cells containing items other than "Vender" unless you add a else clause fixing this. (Same for the text property). Furthermore cells can be empty. In this case item (name) is null which results in a NullPointerException in your code.

    – fabian
    Nov 25 '18 at 11:40



















Cannot reproduce this error. The only compiler issue in your code seems to be that listViewArtigosMercado is not declared in FXMLDocumentController. BTW: Note that multiple items can be used with the same ListCell. You may end up with cells containing items other than "Vender" unless you add a else clause fixing this. (Same for the text property). Furthermore cells can be empty. In this case item (name) is null which results in a NullPointerException in your code.

– fabian
Nov 25 '18 at 11:40







Cannot reproduce this error. The only compiler issue in your code seems to be that listViewArtigosMercado is not declared in FXMLDocumentController. BTW: Note that multiple items can be used with the same ListCell. You may end up with cells containing items other than "Vender" unless you add a else clause fixing this. (Same for the text property). Furthermore cells can be empty. In this case item (name) is null which results in a NullPointerException in your code.

– fabian
Nov 25 '18 at 11:40














3 Answers
3






active

oldest

votes


















-1














Insted of (Node)this use Node.this. You can get the current object of the outer class by calling OuterClass.this.



Then you can just call it's method:



(OuterClass.this).methodOfOuterClass();





share|improve this answer































    3














    Remove the cast and this, it's unambiguous call referring to a parent's method:



    setId("id");


    Look carefully at how you are using setText(name);. The method is from Labeled, but you neither used this nor cast it to that class, simply because there was no need for that. ListCell is a Labeled as well as a Node.






    share|improve this answer

































      0














      Here, As I can see you are using lambda function inside the initialize method as below.



      listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
      @Override
      public void updateItem(String name, boolean empty){
      super.updateItem(name,empty);
      if (name.equals("Vender")){
      setText(name);
      ((Node)this).setId("id");
      }
      }
      });


      Lambda does not treat as the anonymous class. So the scope of the lambda function is limited to that particular enclosing close. this is to reference for that particular scop only. so you can not write this.setId('Id'); inside setCelFactor lambda because it does not contain setId method.



      Here lambda is treated as a child class of the parent class. In your case, its child class of FXMLDocumentController so it has an accessibility of all public method of parent class so you can use setID or other methods direct without any reference.






      share|improve this answer
























      • Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

        – fabian
        Nov 25 '18 at 11:38











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459430%2fjava-this-keyword-inside-new-operator%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      -1














      Insted of (Node)this use Node.this. You can get the current object of the outer class by calling OuterClass.this.



      Then you can just call it's method:



      (OuterClass.this).methodOfOuterClass();





      share|improve this answer




























        -1














        Insted of (Node)this use Node.this. You can get the current object of the outer class by calling OuterClass.this.



        Then you can just call it's method:



        (OuterClass.this).methodOfOuterClass();





        share|improve this answer


























          -1












          -1








          -1







          Insted of (Node)this use Node.this. You can get the current object of the outer class by calling OuterClass.this.



          Then you can just call it's method:



          (OuterClass.this).methodOfOuterClass();





          share|improve this answer













          Insted of (Node)this use Node.this. You can get the current object of the outer class by calling OuterClass.this.



          Then you can just call it's method:



          (OuterClass.this).methodOfOuterClass();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 15:08









          GtomikaGtomika

          364211




          364211

























              3














              Remove the cast and this, it's unambiguous call referring to a parent's method:



              setId("id");


              Look carefully at how you are using setText(name);. The method is from Labeled, but you neither used this nor cast it to that class, simply because there was no need for that. ListCell is a Labeled as well as a Node.






              share|improve this answer






























                3














                Remove the cast and this, it's unambiguous call referring to a parent's method:



                setId("id");


                Look carefully at how you are using setText(name);. The method is from Labeled, but you neither used this nor cast it to that class, simply because there was no need for that. ListCell is a Labeled as well as a Node.






                share|improve this answer




























                  3












                  3








                  3







                  Remove the cast and this, it's unambiguous call referring to a parent's method:



                  setId("id");


                  Look carefully at how you are using setText(name);. The method is from Labeled, but you neither used this nor cast it to that class, simply because there was no need for that. ListCell is a Labeled as well as a Node.






                  share|improve this answer















                  Remove the cast and this, it's unambiguous call referring to a parent's method:



                  setId("id");


                  Look carefully at how you are using setText(name);. The method is from Labeled, but you neither used this nor cast it to that class, simply because there was no need for that. ListCell is a Labeled as well as a Node.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 16:34

























                  answered Nov 24 '18 at 15:06









                  Andrew TobilkoAndrew Tobilko

                  27.5k104285




                  27.5k104285























                      0














                      Here, As I can see you are using lambda function inside the initialize method as below.



                      listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
                      @Override
                      public void updateItem(String name, boolean empty){
                      super.updateItem(name,empty);
                      if (name.equals("Vender")){
                      setText(name);
                      ((Node)this).setId("id");
                      }
                      }
                      });


                      Lambda does not treat as the anonymous class. So the scope of the lambda function is limited to that particular enclosing close. this is to reference for that particular scop only. so you can not write this.setId('Id'); inside setCelFactor lambda because it does not contain setId method.



                      Here lambda is treated as a child class of the parent class. In your case, its child class of FXMLDocumentController so it has an accessibility of all public method of parent class so you can use setID or other methods direct without any reference.






                      share|improve this answer
























                      • Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

                        – fabian
                        Nov 25 '18 at 11:38
















                      0














                      Here, As I can see you are using lambda function inside the initialize method as below.



                      listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
                      @Override
                      public void updateItem(String name, boolean empty){
                      super.updateItem(name,empty);
                      if (name.equals("Vender")){
                      setText(name);
                      ((Node)this).setId("id");
                      }
                      }
                      });


                      Lambda does not treat as the anonymous class. So the scope of the lambda function is limited to that particular enclosing close. this is to reference for that particular scop only. so you can not write this.setId('Id'); inside setCelFactor lambda because it does not contain setId method.



                      Here lambda is treated as a child class of the parent class. In your case, its child class of FXMLDocumentController so it has an accessibility of all public method of parent class so you can use setID or other methods direct without any reference.






                      share|improve this answer
























                      • Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

                        – fabian
                        Nov 25 '18 at 11:38














                      0












                      0








                      0







                      Here, As I can see you are using lambda function inside the initialize method as below.



                      listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
                      @Override
                      public void updateItem(String name, boolean empty){
                      super.updateItem(name,empty);
                      if (name.equals("Vender")){
                      setText(name);
                      ((Node)this).setId("id");
                      }
                      }
                      });


                      Lambda does not treat as the anonymous class. So the scope of the lambda function is limited to that particular enclosing close. this is to reference for that particular scop only. so you can not write this.setId('Id'); inside setCelFactor lambda because it does not contain setId method.



                      Here lambda is treated as a child class of the parent class. In your case, its child class of FXMLDocumentController so it has an accessibility of all public method of parent class so you can use setID or other methods direct without any reference.






                      share|improve this answer













                      Here, As I can see you are using lambda function inside the initialize method as below.



                      listViewArtigosMercado.setCellFactory((ListView<String> param) -> new ListCell<String>(){
                      @Override
                      public void updateItem(String name, boolean empty){
                      super.updateItem(name,empty);
                      if (name.equals("Vender")){
                      setText(name);
                      ((Node)this).setId("id");
                      }
                      }
                      });


                      Lambda does not treat as the anonymous class. So the scope of the lambda function is limited to that particular enclosing close. this is to reference for that particular scop only. so you can not write this.setId('Id'); inside setCelFactor lambda because it does not contain setId method.



                      Here lambda is treated as a child class of the parent class. In your case, its child class of FXMLDocumentController so it has an accessibility of all public method of parent class so you can use setID or other methods direct without any reference.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 24 '18 at 17:03









                      Dhiral KaniyaDhiral Kaniya

                      654622




                      654622













                      • Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

                        – fabian
                        Nov 25 '18 at 11:38



















                      • Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

                        – fabian
                        Nov 25 '18 at 11:38

















                      Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

                      – fabian
                      Nov 25 '18 at 11:38





                      Of course this.setId("id") can be used in the updateItem method. Inside the lambda experssion an anonymus class extending ListCell is created and from the methods of this anonymus class this refers to the ListCell. The cast is unnecessary, but not wrong. (Of course 'Id' is not valid java syntax so this.setId('Id'); won't work)

                      – fabian
                      Nov 25 '18 at 11:38


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459430%2fjava-this-keyword-inside-new-operator%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'