Declaring a method when creating an object
Why first way is correct, but second isn't?
First way:
new Object() {
public void a() {
/*code*/
}
}.a();
Second way:
Object object = new Object() {
public void a() {
/*code*/
}
};
object.a();
And where can I find more information about it?
java object methods
New contributor
add a comment |
Why first way is correct, but second isn't?
First way:
new Object() {
public void a() {
/*code*/
}
}.a();
Second way:
Object object = new Object() {
public void a() {
/*code*/
}
};
object.a();
And where can I find more information about it?
java object methods
New contributor
2
a
is only in scope locally. When you try to access it in the second block, it is out of scope
– GBlodgett
2 hours ago
2
Becausea()
is not a method of theObject
class
– Federico Peralta Schaffner
1 hour ago
add a comment |
Why first way is correct, but second isn't?
First way:
new Object() {
public void a() {
/*code*/
}
}.a();
Second way:
Object object = new Object() {
public void a() {
/*code*/
}
};
object.a();
And where can I find more information about it?
java object methods
New contributor
Why first way is correct, but second isn't?
First way:
new Object() {
public void a() {
/*code*/
}
}.a();
Second way:
Object object = new Object() {
public void a() {
/*code*/
}
};
object.a();
And where can I find more information about it?
java object methods
java object methods
New contributor
New contributor
edited 1 hour ago
ernest_k
19.7k41942
19.7k41942
New contributor
asked 2 hours ago
Sekonoishi Kamiki
311
311
New contributor
New contributor
2
a
is only in scope locally. When you try to access it in the second block, it is out of scope
– GBlodgett
2 hours ago
2
Becausea()
is not a method of theObject
class
– Federico Peralta Schaffner
1 hour ago
add a comment |
2
a
is only in scope locally. When you try to access it in the second block, it is out of scope
– GBlodgett
2 hours ago
2
Becausea()
is not a method of theObject
class
– Federico Peralta Schaffner
1 hour ago
2
2
a
is only in scope locally. When you try to access it in the second block, it is out of scope– GBlodgett
2 hours ago
a
is only in scope locally. When you try to access it in the second block, it is out of scope– GBlodgett
2 hours ago
2
2
Because
a()
is not a method of the Object
class– Federico Peralta Schaffner
1 hour ago
Because
a()
is not a method of the Object
class– Federico Peralta Schaffner
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
In second option you assign your new object to a reference of type Object
because of this only methods defined in java.lang.Object
could be called on that reference.
And in first option you basically create new object of anonymous class that extends java.lang.Object
. And that anonymous class has additional method a()
that is why you can call it.
add a comment |
java.lang.Object
has no a
methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} }
does (1).
Use local variable type inference (var
) to make the second option as valid as the first one.
var object = new Object() {
public void a() {}
};
object.a();
6
Interesting to know that thevar
keyword changes things here...
– ernest_k
1 hour ago
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
});
}
});
Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53960664%2fdeclaring-a-method-when-creating-an-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In second option you assign your new object to a reference of type Object
because of this only methods defined in java.lang.Object
could be called on that reference.
And in first option you basically create new object of anonymous class that extends java.lang.Object
. And that anonymous class has additional method a()
that is why you can call it.
add a comment |
In second option you assign your new object to a reference of type Object
because of this only methods defined in java.lang.Object
could be called on that reference.
And in first option you basically create new object of anonymous class that extends java.lang.Object
. And that anonymous class has additional method a()
that is why you can call it.
add a comment |
In second option you assign your new object to a reference of type Object
because of this only methods defined in java.lang.Object
could be called on that reference.
And in first option you basically create new object of anonymous class that extends java.lang.Object
. And that anonymous class has additional method a()
that is why you can call it.
In second option you assign your new object to a reference of type Object
because of this only methods defined in java.lang.Object
could be called on that reference.
And in first option you basically create new object of anonymous class that extends java.lang.Object
. And that anonymous class has additional method a()
that is why you can call it.
answered 1 hour ago
Ivan
4,7541721
4,7541721
add a comment |
add a comment |
java.lang.Object
has no a
methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} }
does (1).
Use local variable type inference (var
) to make the second option as valid as the first one.
var object = new Object() {
public void a() {}
};
object.a();
6
Interesting to know that thevar
keyword changes things here...
– ernest_k
1 hour ago
add a comment |
java.lang.Object
has no a
methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} }
does (1).
Use local variable type inference (var
) to make the second option as valid as the first one.
var object = new Object() {
public void a() {}
};
object.a();
6
Interesting to know that thevar
keyword changes things here...
– ernest_k
1 hour ago
add a comment |
java.lang.Object
has no a
methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} }
does (1).
Use local variable type inference (var
) to make the second option as valid as the first one.
var object = new Object() {
public void a() {}
};
object.a();
java.lang.Object
has no a
methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} }
does (1).
Use local variable type inference (var
) to make the second option as valid as the first one.
var object = new Object() {
public void a() {}
};
object.a();
edited 1 hour ago
answered 1 hour ago
Andrew Tobilko
25.6k104184
25.6k104184
6
Interesting to know that thevar
keyword changes things here...
– ernest_k
1 hour ago
add a comment |
6
Interesting to know that thevar
keyword changes things here...
– ernest_k
1 hour ago
6
6
Interesting to know that the
var
keyword changes things here...– ernest_k
1 hour ago
Interesting to know that the
var
keyword changes things here...– ernest_k
1 hour ago
add a comment |
Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.
Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.
Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.
Sekonoishi Kamiki is a new contributor. Be nice, and check out our Code of Conduct.
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53960664%2fdeclaring-a-method-when-creating-an-object%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
2
a
is only in scope locally. When you try to access it in the second block, it is out of scope– GBlodgett
2 hours ago
2
Because
a()
is not a method of theObject
class– Federico Peralta Schaffner
1 hour ago