Java - this keyword inside new operator
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
add a comment |
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
Cannot reproduce this error. The only compiler issue in your code seems to be thatlistViewArtigosMercado
is not declared inFXMLDocumentController
. BTW: Note that multiple items can be used with the sameListCell
. You may end up with cells containing items other than"Vender"
unless you add aelse
clause fixing this. (Same for thetext
property). Furthermore cells can be empty. In this caseitem
(name
) isnull
which results in aNullPointerException
in your code.
– fabian
Nov 25 '18 at 11:40
add a comment |
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
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
java inheritance javafx this anonymous-class
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 thatlistViewArtigosMercado
is not declared inFXMLDocumentController
. BTW: Note that multiple items can be used with the sameListCell
. You may end up with cells containing items other than"Vender"
unless you add aelse
clause fixing this. (Same for thetext
property). Furthermore cells can be empty. In this caseitem
(name
) isnull
which results in aNullPointerException
in your code.
– fabian
Nov 25 '18 at 11:40
add a comment |
Cannot reproduce this error. The only compiler issue in your code seems to be thatlistViewArtigosMercado
is not declared inFXMLDocumentController
. BTW: Note that multiple items can be used with the sameListCell
. You may end up with cells containing items other than"Vender"
unless you add aelse
clause fixing this. (Same for thetext
property). Furthermore cells can be empty. In this caseitem
(name
) isnull
which results in aNullPointerException
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
add a comment |
3 Answers
3
active
oldest
votes
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();
add a comment |
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
.
add a comment |
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.
Of coursethis.setId("id")
can be used in theupdateItem
method. Inside the lambda experssion an anonymus class extendingListCell
is created and from the methods of this anonymus classthis
refers to theListCell
. The cast is unnecessary, but not wrong. (Of course'Id'
is not valid java syntax sothis.setId('Id');
won't work)
– fabian
Nov 25 '18 at 11:38
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
add a comment |
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();
add a comment |
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();
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();
answered Nov 24 '18 at 15:08
GtomikaGtomika
364211
364211
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Nov 24 '18 at 16:34
answered Nov 24 '18 at 15:06
Andrew TobilkoAndrew Tobilko
27.5k104285
27.5k104285
add a comment |
add a comment |
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.
Of coursethis.setId("id")
can be used in theupdateItem
method. Inside the lambda experssion an anonymus class extendingListCell
is created and from the methods of this anonymus classthis
refers to theListCell
. The cast is unnecessary, but not wrong. (Of course'Id'
is not valid java syntax sothis.setId('Id');
won't work)
– fabian
Nov 25 '18 at 11:38
add a comment |
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.
Of coursethis.setId("id")
can be used in theupdateItem
method. Inside the lambda experssion an anonymus class extendingListCell
is created and from the methods of this anonymus classthis
refers to theListCell
. The cast is unnecessary, but not wrong. (Of course'Id'
is not valid java syntax sothis.setId('Id');
won't work)
– fabian
Nov 25 '18 at 11:38
add a comment |
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.
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.
answered Nov 24 '18 at 17:03
Dhiral KaniyaDhiral Kaniya
654622
654622
Of coursethis.setId("id")
can be used in theupdateItem
method. Inside the lambda experssion an anonymus class extendingListCell
is created and from the methods of this anonymus classthis
refers to theListCell
. The cast is unnecessary, but not wrong. (Of course'Id'
is not valid java syntax sothis.setId('Id');
won't work)
– fabian
Nov 25 '18 at 11:38
add a comment |
Of coursethis.setId("id")
can be used in theupdateItem
method. Inside the lambda experssion an anonymus class extendingListCell
is created and from the methods of this anonymus classthis
refers to theListCell
. The cast is unnecessary, but not wrong. (Of course'Id'
is not valid java syntax sothis.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Cannot reproduce this error. The only compiler issue in your code seems to be that
listViewArtigosMercado
is not declared inFXMLDocumentController
. BTW: Note that multiple items can be used with the sameListCell
. You may end up with cells containing items other than"Vender"
unless you add aelse
clause fixing this. (Same for thetext
property). Furthermore cells can be empty. In this caseitem
(name
) isnull
which results in aNullPointerException
in your code.– fabian
Nov 25 '18 at 11:40