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 anonymous-class anonymous-inner-class
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 anonymous-class anonymous-inner-class
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 anonymous-class anonymous-inner-class
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 anonymous-class anonymous-inner-class
java object methods anonymous-class anonymous-inner-class
edited Dec 28 '18 at 18:14
Andrew Tobilko
26.9k104285
26.9k104285
asked Dec 28 '18 at 15:21
Sekonoishi KamikiSekonoishi Kamiki
14212
14212
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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 Java 10's 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();
9
Interesting to know that thevar
keyword changes things here...
– ernest_k
Dec 28 '18 at 15:44
1
Note thatvar
was introduced in Java 10 and will not be available in lower versions.
– nasch
Dec 28 '18 at 20:03
5
@ernest_k It's really not that hard or interesting. By sayingObject object =
you are downcasting to anObject
,var
givesobject
its actual correct type that hasa()
defined.
– Sombrero Chicken
Dec 28 '18 at 20:41
2
@SombreroChicken I understand the effect ofvar
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know thatvar
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!
– ernest_k
Dec 28 '18 at 20:46
2
You can also declare an interface that has thea()
method and create an anonymous object from that instead ofObject
, which would work in lower versions that don't supportvar
. E.g.,private interface HasA { public void a(); }
thenHasA object = new HasA() { public void a() { ... } };
.
– jpmc26
Dec 28 '18 at 23:33
|
show 1 more comment
In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object
. That anonymous class has the additional method a()
, which is why you can call it.
add a comment |
Java is statically typed. When you say object.a()
it is looking for the method a
in the Object
class which is not present. Hence it does not compile.
What you can do is get the method of object
using reflection as shown below :
Object object = new Object() {
public void a() {
System.out.println("In a");
}
}
Method method = object.getClass().getDeclaredMethod("a");
method.invoke(object, null);
This would print
In a
2
That's not true. Java uses static typing (you can see it in your example -Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here
– MatthewRock
Dec 28 '18 at 19:39
2
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
1
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
add a comment |
Don't worry, you will have to do a bit of correction
Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -
public class demo {
public static void main(String args) {
new Object() {
public void a() {
/*code*/
System.out.println("Hello");
}
}.a();
}
}
But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -
interface Object
{
public void a();
}
class demo {
public static void main(String args) {
Object object = new Object() {
public void a() {
System.out.println("Hello");
}
}; object.a();
}
}
I hope it will help a bit.
1
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
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%2f53960664%2fdeclaring-a-method-when-creating-an-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 Java 10's 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();
9
Interesting to know that thevar
keyword changes things here...
– ernest_k
Dec 28 '18 at 15:44
1
Note thatvar
was introduced in Java 10 and will not be available in lower versions.
– nasch
Dec 28 '18 at 20:03
5
@ernest_k It's really not that hard or interesting. By sayingObject object =
you are downcasting to anObject
,var
givesobject
its actual correct type that hasa()
defined.
– Sombrero Chicken
Dec 28 '18 at 20:41
2
@SombreroChicken I understand the effect ofvar
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know thatvar
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!
– ernest_k
Dec 28 '18 at 20:46
2
You can also declare an interface that has thea()
method and create an anonymous object from that instead ofObject
, which would work in lower versions that don't supportvar
. E.g.,private interface HasA { public void a(); }
thenHasA object = new HasA() { public void a() { ... } };
.
– jpmc26
Dec 28 '18 at 23:33
|
show 1 more 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 Java 10's 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();
9
Interesting to know that thevar
keyword changes things here...
– ernest_k
Dec 28 '18 at 15:44
1
Note thatvar
was introduced in Java 10 and will not be available in lower versions.
– nasch
Dec 28 '18 at 20:03
5
@ernest_k It's really not that hard or interesting. By sayingObject object =
you are downcasting to anObject
,var
givesobject
its actual correct type that hasa()
defined.
– Sombrero Chicken
Dec 28 '18 at 20:41
2
@SombreroChicken I understand the effect ofvar
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know thatvar
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!
– ernest_k
Dec 28 '18 at 20:46
2
You can also declare an interface that has thea()
method and create an anonymous object from that instead ofObject
, which would work in lower versions that don't supportvar
. E.g.,private interface HasA { public void a(); }
thenHasA object = new HasA() { public void a() { ... } };
.
– jpmc26
Dec 28 '18 at 23:33
|
show 1 more 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 Java 10's 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 Java 10's 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 Jan 2 at 19:27
answered Dec 28 '18 at 15:36
Andrew TobilkoAndrew Tobilko
26.9k104285
26.9k104285
9
Interesting to know that thevar
keyword changes things here...
– ernest_k
Dec 28 '18 at 15:44
1
Note thatvar
was introduced in Java 10 and will not be available in lower versions.
– nasch
Dec 28 '18 at 20:03
5
@ernest_k It's really not that hard or interesting. By sayingObject object =
you are downcasting to anObject
,var
givesobject
its actual correct type that hasa()
defined.
– Sombrero Chicken
Dec 28 '18 at 20:41
2
@SombreroChicken I understand the effect ofvar
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know thatvar
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!
– ernest_k
Dec 28 '18 at 20:46
2
You can also declare an interface that has thea()
method and create an anonymous object from that instead ofObject
, which would work in lower versions that don't supportvar
. E.g.,private interface HasA { public void a(); }
thenHasA object = new HasA() { public void a() { ... } };
.
– jpmc26
Dec 28 '18 at 23:33
|
show 1 more comment
9
Interesting to know that thevar
keyword changes things here...
– ernest_k
Dec 28 '18 at 15:44
1
Note thatvar
was introduced in Java 10 and will not be available in lower versions.
– nasch
Dec 28 '18 at 20:03
5
@ernest_k It's really not that hard or interesting. By sayingObject object =
you are downcasting to anObject
,var
givesobject
its actual correct type that hasa()
defined.
– Sombrero Chicken
Dec 28 '18 at 20:41
2
@SombreroChicken I understand the effect ofvar
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know thatvar
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!
– ernest_k
Dec 28 '18 at 20:46
2
You can also declare an interface that has thea()
method and create an anonymous object from that instead ofObject
, which would work in lower versions that don't supportvar
. E.g.,private interface HasA { public void a(); }
thenHasA object = new HasA() { public void a() { ... } };
.
– jpmc26
Dec 28 '18 at 23:33
9
9
Interesting to know that the
var
keyword changes things here...– ernest_k
Dec 28 '18 at 15:44
Interesting to know that the
var
keyword changes things here...– ernest_k
Dec 28 '18 at 15:44
1
1
Note that
var
was introduced in Java 10 and will not be available in lower versions.– nasch
Dec 28 '18 at 20:03
Note that
var
was introduced in Java 10 and will not be available in lower versions.– nasch
Dec 28 '18 at 20:03
5
5
@ernest_k It's really not that hard or interesting. By saying
Object object =
you are downcasting to an Object
, var
gives object
its actual correct type that has a()
defined.– Sombrero Chicken
Dec 28 '18 at 20:41
@ernest_k It's really not that hard or interesting. By saying
Object object =
you are downcasting to an Object
, var
gives object
its actual correct type that has a()
defined.– Sombrero Chicken
Dec 28 '18 at 20:41
2
2
@SombreroChicken I understand the effect of
var
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!– ernest_k
Dec 28 '18 at 20:46
@SombreroChicken I understand the effect of
var
here. But beside the first syntax in the question, there was no other way (that I know) to have a static reference to the anonymous type. Now I know that var
can be used for that. Even more, it allows one to make multiple method calls from the anonymous class. I find that strangely new and interesting!– ernest_k
Dec 28 '18 at 20:46
2
2
You can also declare an interface that has the
a()
method and create an anonymous object from that instead of Object
, which would work in lower versions that don't support var
. E.g., private interface HasA { public void a(); }
then HasA object = new HasA() { public void a() { ... } };
.– jpmc26
Dec 28 '18 at 23:33
You can also declare an interface that has the
a()
method and create an anonymous object from that instead of Object
, which would work in lower versions that don't support var
. E.g., private interface HasA { public void a(); }
then HasA object = new HasA() { public void a() { ... } };
.– jpmc26
Dec 28 '18 at 23:33
|
show 1 more comment
In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object
. That anonymous class has the additional method a()
, which is why you can call it.
add a comment |
In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object
. That anonymous class has the additional method a()
, which is why you can call it.
add a comment |
In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object
. That anonymous class has the additional method a()
, which is why you can call it.
In the 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 the first option, you basically create new object of anonymous class that extends java.lang.Object
. That anonymous class has the additional method a()
, which is why you can call it.
edited Dec 28 '18 at 20:20
MTCoster
3,06621940
3,06621940
answered Dec 28 '18 at 15:24
IvanIvan
5,0831821
5,0831821
add a comment |
add a comment |
Java is statically typed. When you say object.a()
it is looking for the method a
in the Object
class which is not present. Hence it does not compile.
What you can do is get the method of object
using reflection as shown below :
Object object = new Object() {
public void a() {
System.out.println("In a");
}
}
Method method = object.getClass().getDeclaredMethod("a");
method.invoke(object, null);
This would print
In a
2
That's not true. Java uses static typing (you can see it in your example -Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here
– MatthewRock
Dec 28 '18 at 19:39
2
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
1
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
add a comment |
Java is statically typed. When you say object.a()
it is looking for the method a
in the Object
class which is not present. Hence it does not compile.
What you can do is get the method of object
using reflection as shown below :
Object object = new Object() {
public void a() {
System.out.println("In a");
}
}
Method method = object.getClass().getDeclaredMethod("a");
method.invoke(object, null);
This would print
In a
2
That's not true. Java uses static typing (you can see it in your example -Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here
– MatthewRock
Dec 28 '18 at 19:39
2
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
1
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
add a comment |
Java is statically typed. When you say object.a()
it is looking for the method a
in the Object
class which is not present. Hence it does not compile.
What you can do is get the method of object
using reflection as shown below :
Object object = new Object() {
public void a() {
System.out.println("In a");
}
}
Method method = object.getClass().getDeclaredMethod("a");
method.invoke(object, null);
This would print
In a
Java is statically typed. When you say object.a()
it is looking for the method a
in the Object
class which is not present. Hence it does not compile.
What you can do is get the method of object
using reflection as shown below :
Object object = new Object() {
public void a() {
System.out.println("In a");
}
}
Method method = object.getClass().getDeclaredMethod("a");
method.invoke(object, null);
This would print
In a
edited Dec 28 '18 at 19:51
answered Dec 28 '18 at 19:31
fastcodejavafastcodejava
24k19109162
24k19109162
2
That's not true. Java uses static typing (you can see it in your example -Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here
– MatthewRock
Dec 28 '18 at 19:39
2
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
1
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
add a comment |
2
That's not true. Java uses static typing (you can see it in your example -Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here
– MatthewRock
Dec 28 '18 at 19:39
2
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
1
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
2
2
That's not true. Java uses static typing (you can see it in your example -
Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here– MatthewRock
Dec 28 '18 at 19:39
That's not true. Java uses static typing (you can see it in your example -
Object object
- you just declared its type!). It uses polymorphism to determine which method should be called, but that's a different thing than dynamic typing. Some debate about dynamic vs static can be found here– MatthewRock
Dec 28 '18 at 19:39
2
2
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
you should have pointed out that calling the method through reflection is the last thing OP wants :)
– Andrew Tobilko
Dec 28 '18 at 20:17
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@MatthewRock The JVM does support dynamic dispatch, however.
– Solomon Ucko
Dec 29 '18 at 19:27
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
@SolomonUcko JVM is not Java. Dynamic dispatch is also not something that makes a language a static one. C++ has dynamic dispatch for virtual methods, but is still a static language.
– MatthewRock
Dec 29 '18 at 20:59
1
1
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
@MatthewRock What exactly was wrong?
– fastcodejava
Dec 30 '18 at 4:35
add a comment |
Don't worry, you will have to do a bit of correction
Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -
public class demo {
public static void main(String args) {
new Object() {
public void a() {
/*code*/
System.out.println("Hello");
}
}.a();
}
}
But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -
interface Object
{
public void a();
}
class demo {
public static void main(String args) {
Object object = new Object() {
public void a() {
System.out.println("Hello");
}
}; object.a();
}
}
I hope it will help a bit.
1
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
add a comment |
Don't worry, you will have to do a bit of correction
Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -
public class demo {
public static void main(String args) {
new Object() {
public void a() {
/*code*/
System.out.println("Hello");
}
}.a();
}
}
But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -
interface Object
{
public void a();
}
class demo {
public static void main(String args) {
Object object = new Object() {
public void a() {
System.out.println("Hello");
}
}; object.a();
}
}
I hope it will help a bit.
1
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
add a comment |
Don't worry, you will have to do a bit of correction
Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -
public class demo {
public static void main(String args) {
new Object() {
public void a() {
/*code*/
System.out.println("Hello");
}
}.a();
}
}
But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -
interface Object
{
public void a();
}
class demo {
public static void main(String args) {
Object object = new Object() {
public void a() {
System.out.println("Hello");
}
}; object.a();
}
}
I hope it will help a bit.
Don't worry, you will have to do a bit of correction
Both are ways to access the private member of a class. By using first way you don' have to pre-declare the method.ex: -
public class demo {
public static void main(String args) {
new Object() {
public void a() {
/*code*/
System.out.println("Hello");
}
}.a();
}
}
But by using second way you will have to explicitly declare the method a(); either in abstract class or in interface then you can override it. like: -
interface Object
{
public void a();
}
class demo {
public static void main(String args) {
Object object = new Object() {
public void a() {
System.out.println("Hello");
}
}; object.a();
}
}
I hope it will help a bit.
answered Dec 30 '18 at 12:28
MAYANKMAYANK
13
13
1
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
add a comment |
1
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
1
1
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
"to access the private member of a class" what's the private member?
– Andrew Tobilko
Dec 30 '18 at 12:46
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%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