 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Martin Gerner Guest
|
Posted: Thu Apr 20, 2006 2:12 pm Post subject: static method and private class |
|
|
Hi, I've run into an error that I don't understand how to correct...
public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a static
context.
Commenting out the PrivateClass anobject.. line makes it compile fine.
I can obviously create objects, but why not objects of a private class? It
has to do with the method being static, of course, but I can't see how. |
|
| Back to top |
|
 |
Martin Gerner Guest
|
Posted: Thu Apr 20, 2006 3:03 pm Post subject: Re: static method and private class |
|
|
Simon <count.numbers (AT) web (DOT) de> wrote in
news:4apl3bFtjsvaU1 (AT) news (DOT) dfncis.de:
| Quote: | Hi,
public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a
static context.
Commenting out the PrivateClass anobject.. line makes it compile
fine. I can obviously create objects, but why not objects of a
private class? It has to do with the method being static, of course,
but I can't see how.
It has nothing to do with the class being private, but rather with the
class being non-static. You have two options:
1. Declare PrivateClass static ("private static class PrivateClass")
2. Call the constructor from a non-static context.
Test test = new Test();
PrivateClass anobject = test.new PrivateClass();
This may look weird and I believe it isn't used much. If you declare
DoSomething() to be non-static, the "new PrivateClass()" automatically
becomes "this.new PrivateClass()" which gives you the non-static
context for the construcor.
The reason for this behaviour is that from within a non-static inner
class you can always reference an instance of the outer class (in this
case by Test.this), which is only possible if such a reference exists
when the instance of the inner class is created.
|
Hm, that's crap. :/
Actually, I wasn't planning to create objects of the 'Test'class, but am
rather using it to hold a bunch of static methods. These need to send
data between themselves, which I was going to place in the inner private
class. I could, of course, make everything non-static and create an
instance of the Test class, but it seems a bit awkward.. doesn't seem to
be much else I can do, though. (Except put the private class as public in
a new java file, but that is even more awkward :p) |
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Thu Apr 20, 2006 3:12 pm Post subject: Re: static method and private class |
|
|
Martin Gerner wrote:
| Quote: | Hi, I've run into an error that I don't understand how to correct...
public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a static
context.
Commenting out the PrivateClass anobject.. line makes it compile fine.
I can obviously create objects, but why not objects of a private class? It
has to do with the method being static, of course, but I can't see how.
|
The problem is that PrivateClass, having not been declared static, needs
a reference to an object of the surrounding class, Test.
There are two solutions. If you want each PrivateClass to belong to a
particular Test, you need to create an instance of Test and use it to
create a PrivateClass. If you do not need that association you can
declare PrivateClass static.
Patricia |
|
| Back to top |
|
 |
Simon Guest
|
Posted: Thu Apr 20, 2006 3:12 pm Post subject: Re: static method and private class |
|
|
Hi,
| Quote: | public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a static
context.
Commenting out the PrivateClass anobject.. line makes it compile fine.
I can obviously create objects, but why not objects of a private class? It
has to do with the method being static, of course, but I can't see how.
|
It has nothing to do with the class being private, but rather with the class
being non-static. You have two options:
1. Declare PrivateClass static ("private static class PrivateClass")
2. Call the constructor from a non-static context.
Test test = new Test();
PrivateClass anobject = test.new PrivateClass();
This may look weird and I believe it isn't used much. If you declare
DoSomething() to be non-static, the "new PrivateClass()" automatically becomes
"this.new PrivateClass()" which gives you the non-static context for the construcor.
The reason for this behaviour is that from within a non-static inner class you
can always reference an instance of the outer class (in this case by Test.this),
which is only possible if such a reference exists when the instance of the inner
class is created.
Cheers,
Simon
BTW: What's the term for "non-static" once again? |
|
| Back to top |
|
 |
Philipp Leitner Guest
|
Posted: Thu Apr 20, 2006 4:12 pm Post subject: Re: static method and private class |
|
|
Well, if you want to send "data" between those static objects they
clearly should not be static at all, but standard objects (singletons
perhaps, but still actual concrete objects)!
/philipp
Martin Gerner schrieb:
| Quote: | Simon <count.numbers (AT) web (DOT) de> wrote in
news:4apl3bFtjsvaU1 (AT) news (DOT) dfncis.de:
Hi,
public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a
static context.
Commenting out the PrivateClass anobject.. line makes it compile
fine. I can obviously create objects, but why not objects of a
private class? It has to do with the method being static, of course,
but I can't see how.
It has nothing to do with the class being private, but rather with the
class being non-static. You have two options:
1. Declare PrivateClass static ("private static class PrivateClass")
2. Call the constructor from a non-static context.
Test test = new Test();
PrivateClass anobject = test.new PrivateClass();
This may look weird and I believe it isn't used much. If you declare
DoSomething() to be non-static, the "new PrivateClass()" automatically
becomes "this.new PrivateClass()" which gives you the non-static
context for the construcor.
The reason for this behaviour is that from within a non-static inner
class you can always reference an instance of the outer class (in this
case by Test.this), which is only possible if such a reference exists
when the instance of the inner class is created.
Hm, that's crap. :/
Actually, I wasn't planning to create objects of the 'Test'class, but am
rather using it to hold a bunch of static methods. These need to send
data between themselves, which I was going to place in the inner private
class. I could, of course, make everything non-static and create an
instance of the Test class, but it seems a bit awkward.. doesn't seem to
be much else I can do, though. (Except put the private class as public in
a new java file, but that is even more awkward :p) |
|
|
| Back to top |
|
 |
Simon Guest
|
Posted: Thu Apr 20, 2006 4:12 pm Post subject: Re: static method and private class |
|
|
Martin Gerner wrote:
| Quote: | Hm, that's crap. :/
|
What is crap, my answer or Java? If you mean Java is crap, then my answer is:
This is the only way it makes sense. Instances of inner classes have a reference
to the outer class -> you cannot create them from a static context.
Static inner classes don't have such a reference -> you can create them from
everywhere.
| Quote: | Actually, I wasn't planning to create objects of the 'Test'class, but am
rather using it to hold a bunch of static methods. These need to send
data between themselves, which I was going to place in the inner private
class. I could, of course, make everything non-static and create an
instance of the Test class, but it seems a bit awkward.. doesn't seem to
be much else I can do, though. (Except put the private class as public in
a new java file, but that is even more awkward :p)
|
I'm not sure I understand. Especially, I don't see why making the private class
public and putting it into a file of its own solves the problem.
What do you mean by "send data"? If you want to maintain some state of "Test",
then you can use static fields in Test.
If you want to pass parameters between static methods calling each other then
you can declare your inner class static. If Test really only has static methods,
then certainly there is no point in making the inner class non-static because
there is nothing useful the inner class can access in the instance of the outer
class.
I'm not sure I'm getting the point, though. |
|
| Back to top |
|
 |
Martin Gerner Guest
|
Posted: Thu Apr 20, 2006 4:25 pm Post subject: Re: static method and private class |
|
|
Simon <count.numbers (AT) web (DOT) de> wrote in
news:4appraFu38l7U1 (AT) news (DOT) dfncis.de:
| Quote: | Martin Gerner wrote:
Hm, that's crap. :/
What is crap, my answer or Java? If you mean Java is crap, then my
answer is: This is the only way it makes sense. Instances of inner
classes have a reference to the outer class -> you cannot create them
from a static context.
|
What I meant was that it was crap that I couldn't get it to work as I would
have liked it to. No disrespect intended - on the contrary, you gave a
quick and good response. (both you and Patricia)
I am using the methods as non-static now, anyways. Perhaps that is better,
I don't know, but the main point is that it works, and how I would have
liked it wouldn't have worked.
/Martin Gerner |
|
| Back to top |
|
 |
Hendrik Maryns Guest
|
Posted: Thu Apr 20, 2006 5:12 pm Post subject: Re: static method and private class |
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Martin Gerner schreef:
| Quote: | Simon <count.numbers (AT) web (DOT) de> wrote in
news:4apl3bFtjsvaU1 (AT) news (DOT) dfncis.de:
Hi,
public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a
static context.
Commenting out the PrivateClass anobject.. line makes it compile
fine. I can obviously create objects, but why not objects of a
private class? It has to do with the method being static, of course,
but I can't see how.
It has nothing to do with the class being private, but rather with the
class being non-static. You have two options:
1. Declare PrivateClass static ("private static class PrivateClass")
2. Call the constructor from a non-static context.
Test test = new Test();
PrivateClass anobject = test.new PrivateClass();
This may look weird and I believe it isn't used much. If you declare
DoSomething() to be non-static, the "new PrivateClass()" automatically
becomes "this.new PrivateClass()" which gives you the non-static
context for the construcor.
The reason for this behaviour is that from within a non-static inner
class you can always reference an instance of the outer class (in this
case by Test.this), which is only possible if such a reference exists
when the instance of the inner class is created.
Hm, that's crap. :/
|
It isn’t.
| Quote: | Actually, I wasn't planning to create objects of the 'Test'class, but am
rather using it to hold a bunch of static methods. These need to send
data between themselves, which I was going to place in the inner private
class. I could, of course, make everything non-static and create an
instance of the Test class, but it seems a bit awkward.. doesn't seem to
be much else I can do, though. (Except put the private class as public in
a new java file, but that is even more awkward :p)
|
I think you just want to declare PrivateClass as being static.
H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFER7KAe+7xMGD3itQRAsbEAJ0aZ6EOplwlUu9cFtUwD2PbYN60EwCfanP8
ueMNDDfV2Y4sIsOaZSfxC2Y=
=c6Ez
-----END PGP SIGNATURE----- |
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Thu Apr 20, 2006 5:12 pm Post subject: Re: static method and private class |
|
|
On Thu, 20 Apr 2006 14:12:05 +0000 (UTC), Martin Gerner
<martin.gerner (AT) nospam (DOT) com> wrote, quoted or indirectly quoted someone
who said :
| Quote: | non-static variable this cannot be referenced from a static
context.
|
see
http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching. |
|
| Back to top |
|
 |
Andrew McDonagh Guest
|
Posted: Thu Apr 20, 2006 5:12 pm Post subject: Re: static method and private class |
|
|
Martin Gerner wrote:
| Quote: | Simon <count.numbers (AT) web (DOT) de> wrote in
news:4apl3bFtjsvaU1 (AT) news (DOT) dfncis.de:
Hi,
public class Test{
private class PrivateClass{
public int somedata;
}
static public void DoSomething(){
String astring = new String();
PrivateClass anobject = new PrivateClass();
}
}
When compiling above code, it gives me the following error:
Test.java:8: non-static variable this cannot be referenced from a
static context.
Commenting out the PrivateClass anobject.. line makes it compile
fine. I can obviously create objects, but why not objects of a
private class? It has to do with the method being static, of course,
but I can't see how.
It has nothing to do with the class being private, but rather with the
class being non-static. You have two options:
1. Declare PrivateClass static ("private static class PrivateClass")
2. Call the constructor from a non-static context.
Test test = new Test();
PrivateClass anobject = test.new PrivateClass();
This may look weird and I believe it isn't used much. If you declare
DoSomething() to be non-static, the "new PrivateClass()" automatically
becomes "this.new PrivateClass()" which gives you the non-static
context for the construcor.
The reason for this behaviour is that from within a non-static inner
class you can always reference an instance of the outer class (in this
case by Test.this), which is only possible if such a reference exists
when the instance of the inner class is created.
Hm, that's crap. :/
|
no its intentional for many good reasons.
| Quote: | Actually, I wasn't planning to create objects of the 'Test'class, but am
rather using it to hold a bunch of static methods. These need to send
data between themselves, which I was going to place in the inner private
class. I could, of course, make everything non-static and create an
instance of the Test class, but it seems a bit awkward.. doesn't seem to
be much else I can do, though. (Except put the private class as public in
a new java file, but that is even more awkward :p)
|
The inner class you have written is called a Inner class
If you use the static keyword you have a Static Nested class - these do
not have the reference to their enclosing class.
class EnclosingClass {
...
static class StaticNestedClass {
...
}
class InnerClass {
...
}
}
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html |
|
| Back to top |
|
 |
Martin Gerner Guest
|
Posted: Thu Apr 20, 2006 5:57 pm Post subject: Re: static method and private class |
|
|
Patricia Shanahan <pats (AT) acm (DOT) org> wrote in
news:wlP1g.253$Vn.42 (AT) newsread4 (DOT) news.pas.earthlink.net:
| Quote: | I don't understand why you didn't like declaring the private class
"static". It seems to me that that would have done exactly what you
expected.
|
Hm. Yes, I tried that now, and it actually worked perfectly, just as I
wanted. What I thought would happen when declaring the private class static
was that the fields contained would have become static as well. Since I
needed multiple 'PrivateClass' objects with different data, that wouldn't
have worked.
But ok, thank you, I believe that I've learned something useful now (Not
to mention that my code became a bit nicer.)
--
Martin Gerner |
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Thu Apr 20, 2006 6:12 pm Post subject: Re: static method and private class |
|
|
Martin Gerner wrote:
| Quote: | Simon <count.numbers (AT) web (DOT) de> wrote in
news:4appraFu38l7U1 (AT) news (DOT) dfncis.de:
Martin Gerner wrote:
Hm, that's crap. :/
What is crap, my answer or Java? If you mean Java is crap, then my
answer is: This is the only way it makes sense. Instances of inner
classes have a reference to the outer class -> you cannot create them
from a static context.
What I meant was that it was crap that I couldn't get it to work as I would
have liked it to. No disrespect intended - on the contrary, you gave a
quick and good response. (both you and Patricia)
I am using the methods as non-static now, anyways. Perhaps that is better,
I don't know, but the main point is that it works, and how I would have
liked it wouldn't have worked.
/Martin Gerner
|
I don't understand why you didn't like declaring the private class
"static". It seems to me that that would have done exactly what you
expected.
Personally, I tend to avoid extensive use of static, but if you are
planning to have a lot of static methods, I don't see the objection to a
static class.
Patricia |
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Thu Apr 20, 2006 7:12 pm Post subject: Re: static method and private class |
|
|
"Simon" <count.numbers (AT) web (DOT) de> wrote in message
news:4apl3bFtjsvaU1 (AT) news (DOT) dfncis.de...
| Quote: | BTW: What's the term for "non-static" once again?
|
Instance.
- Oliver |
|
| Back to top |
|
 |
Hendrik Maryns Guest
|
Posted: Fri Apr 21, 2006 1:12 pm Post subject: Re: static method and private class |
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Martin Gerner schreef:
| Quote: | Patricia Shanahan <pats (AT) acm (DOT) org> wrote in
news:wlP1g.253$Vn.42 (AT) newsread4 (DOT) news.pas.earthlink.net:
I don't understand why you didn't like declaring the private class
"static". It seems to me that that would have done exactly what you
expected.
Hm. Yes, I tried that now, and it actually worked perfectly, just as I
wanted. What I thought would happen when declaring the private class static
was that the fields contained would have become static as well. Since I
needed multiple 'PrivateClass' objects with different data, that wouldn't
have worked.
|
You misunderstood. It still is a perfectly normal class, with instance
methods and objects. The static just means it is not bound to an object
of the enclosing class. A non-static inner class always has a reference
to an enclosing object of the outer class through OuterClass.this. I
found a nice pdf on the net that explains this all very thoroughly:
http://java.sun.com/developer/Books/certification/certbook.pdf.
| Quote: | But ok, thank you, I believe that I've learned something useful now (Not
to mention that my code became a bit nicer.)
|
I hope you did :-)
H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFESM7me+7xMGD3itQRAgi9AJ4zaUB3JM1EaF8UG8tXFDe+NJG85gCfZJAG
GZvGbjWgKmr1G5SKG8/193I=
=1CEZ
-----END PGP SIGNATURE----- |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|