why do we need private static method or member class

Dear java gurus,

I have a question about the use of private and static key words together in a method or inner class.

If we want to hide the method, private is enough. a private static method is sure not intended to be called outside the class. So the only usage I could see is that this private static method is to be called by another static method. For inner class, I see the definition of Entry inner class in java.util.Hashtable, it is static private. I don't know why not just define it as private. Could the static key word do anything better.

Could anybody help me to clear this.

Thanks,

[627 byte] By [cafebara] at [2007-11-27 8:22:00]
# 1

You make a member (be it a method or a class) private when wider access is not appropriate. The static modifier is orthogonal to the access qualifiers. A method is declared to be static if it is associated with the class as a whole rather than instances of the class. For nested classes, the static modifier indicates that instances of the nested class do not have an implicit reference to an instance of the enclosing class.

Hippolytea at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 2
Private and static mean two completely different things. Your question is equivalent to asking, "Why would we want a car to have both air conditioning and a CD player?"
jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks for your answer!But to me it still makes no difference as far as the actual usage is concerned. Anyway you must limited the call to a private static method or the initialization of an inner class to the defining class.
cafebara at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks for your answer!

>

> But to me it still makes no difference as far as the

> actual usage is concerned.

Then you don't understand them.

> Anyway you must limited

> the call to a private static method or the

> initialization of an inner class to the defining

> class.

I have no idea what you're saying or what point you're trying to make.

My previous post stands, unassailed and unassailable.

jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 5

Dear jverd,

I was not answering your question in the above post.

Yes, I totally agree with you that they are completely different things for a CD player and an airconditioner. But what a CD is for if a car can drive without it . I mean if a private modifier could do fine, why use a static. Just for more fun?

cafebara at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 6

> Dear jverd,

>

> I was not answering your question in the above post.

>

> Yes, I totally agree with you that they are

> completely different things for a CD player and an

> airconditioner. But what a CD is for if a car can

> drive without it . I mean if a private modifier could

> do fine, why use a static. Just for more fun?

What don't you get? Private does one thing, and static does something completely different.

If you want to listen to music, installing an air conditioner doesn't help.

jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 7

>What don't you get? Private does one thing, and static does >something completely different.

>If you want to listen to music, installing an air conditioner doesn't help>

Hi, if the private keyword is the airconditioner, do you think you could get music from the static keyword (it acts as the CD player) in the following codes:

class

{

private Entry entry[];

public Entry makeEntries()

{

entry = new Entry[10];

return entry;

}

private static class Entry

{

};

}

cafebara at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 8

Jeff, I think the question they are trying to ask is, in the below code it makes no diff if the method is static or not. You still get a compile error.

class Foo {

public static void main(String[] args) {

Bar.guff();

}

}

class Bar {

private static void guff() {

System.out.println("Hello");

}

}

floundera at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 9

>Jeff, I think the question they are trying to ask is, in the below code it >makes no diff if the method is static or not. You still get a compile error.

>class Foo {

>public static void main(String[] args) {

>Bar.guff();

>}

>}

>class Bar {

>private static void guff() {

>System.out.println("Hello");

>}

>}

Far from it. See the above example.

cafebara at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 10

> >What don't you get? Private does one thing, and

> static does >something completely different.

>

> >If you want to listen to music, installing an air

> conditioner doesn't help>

>

> Hi, if the private keyword is the airconditioner, do

> you think you could get music from the static keyword

> (it acts as the CD player) in the following codes:

You're making no sense and you're trying to stretch the analogy too far.

Private does one thing. If you want that thing, use private.

Static does something completely different and unrelated. If you want that thing, use static.

If you want both things, use private static.

What do you not understand? How can you claim that you understand that they are different, and then ask, "Why do we need static if we have private"? That question makes no sense if you actually do understand that they're different.

jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 11
> Far from it. See the above example.Your question makes no sense to me.
jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 12

> called by another static method. For inner class, I

> see the definition of Entry inner class in

> java.util.Hashtable, it is static private. I don't

> know why not just define it as private. Could the

> static key word do anything better.

As already explained: Static nested classes have no reference to an enclosing instance. Non-static nested classes have a reference to the enclosing instance. If you need a reference to the enclosing instance, use non-static. If you don't need such a reference, use static.

Private has nothing to do with this.

jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 13

cafebar,

You can't possibly think this sentence is coherent.

"Hi, if the private keyword is the airconditioner, do you think you could get music from the static keyword (it acts as the CD player) in the following codes:"

I mean there would be some hope if the code had airconditioners or cd players.

And if that is supposed to be an analogy then what in the world is the relationship between air conditioners and CD players supposed to be?

Please take a deep breath, relax, and try and compose some coherent sentences with perhaps even some coherent and formatted code.

cotton.ma at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 14
> And if that is supposed to be an analogy then what in> the world is the relationship between air> conditioners and CD players supposed to be?He's sexually abusing the analogy I used in reply 2.
jverda at 2007-7-12 20:10:35 > top of Java-index,Java Essentials,New To Java...
# 15

> > And if that is supposed to be an analogy then what

> in

> > the world is the relationship between air

> > conditioners and CD players supposed to be?

>

>

> He's sexually abusing the analogy I used in reply 2.

I see. So you're to blame.

I should have known. :p

cotton.ma at 2007-7-21 22:38:20 > top of Java-index,Java Essentials,New To Java...
# 16

> > > And if that is supposed to be an analogy then

> what

> > in

> > > the world is the relationship between air

> > > conditioners and CD players supposed to be?

> >

> >

> > He's sexually abusing the analogy I used in reply

> 2.

>

> I see. So you're to blame.

>

> I should have known. :p

Are you insinuating that if the subject turns to sexual abuse we should immediately think of jverd?

floundera at 2007-7-21 22:38:20 > top of Java-index,Java Essentials,New To Java...
# 17

> > > > And if that is supposed to be an analogy then

> > what

> > > in

> > > > the world is the relationship between air

> > > > conditioners and CD players supposed to be?

> > >

> > >

> > > He's sexually abusing the analogy I used in

> reply

> > 2.

> >

> > I see. So you're to blame.

> >

> > I should have known. :p

>

> Are you insinuating that if the subject turns to

> sexual abuse we should immediately think of jverd?

I told you before--I thought the goat was a prostitute. Sheesh!

jverda at 2007-7-21 22:38:20 > top of Java-index,Java Essentials,New To Java...
# 18

Hi, you guys,

I am not a native speaker of english and maybe also lack of the humors you have, but I just tried to see if anybody really can say something which will clear my mind. I admit my java knowledge might be a long way behind you, this is why I post the question. Otherwise, who comes here?

cafebara at 2007-7-21 22:38:20 > top of Java-index,Java Essentials,New To Java...
# 19

> Hi, you guys,

>

> I am not a native speaker of english and maybe also

> lack of the humors you have, but I just tried to see

> if anybody really can say something which will clear

> my mind. I admit my java knowledge might be a long

> way behind you, this is why I post the question.

> Otherwise, who comes here?

Please don't whine.

We can't clear your mind because you haven't asked a question that we can decipher yet. Is this possibly an English barrier? Yes. But that can't be helped. You do your best and we do our best.

Okay?

Anyway you'll have to ask a question that we have some hope of discerning what you are talking about from. You haven't done this yet.

cotton.ma at 2007-7-21 22:38:21 > top of Java-index,Java Essentials,New To Java...
# 20

> Hi, you guys,

>

> I am not a native speaker of english and maybe also

> lack of the humors you have, but I just tried to see

> if anybody really can say something which will clear

> my mind. I admit my java knowledge might be a long

> way behind you, this is why I post the question.

> Otherwise, who comes here?

We are trying to help you. We've already answered your question. I don't understand what you're still confused about. It's fine if English isn't your native language, but if you can't find a way to clarify your question, it will be hard to get any different answer from what you've already gotten.

As for the humor, if you don't understand it or don't enjoy it, just ignore it.

jverda at 2007-7-21 22:38:21 > top of Java-index,Java Essentials,New To Java...
# 21
Anyway, thanks and bye
cafebara at 2007-7-21 22:38:21 > top of Java-index,Java Essentials,New To Java...
# 22

> Hi, you guys,

>

> I am not a native speaker of english and maybe also

> lack of the humors you have, but I just tried to see

> if anybody really can say something which will clear

> my mind. I admit my java knowledge might be a long

> way behind you, this is why I post the question.

> Otherwise, who comes here?

Read reply 10 and reply 12.

jverda at 2007-7-21 22:38:21 > top of Java-index,Java Essentials,New To Java...
# 23
> Anyway, thanks and byeOkay bye.
cotton.ma at 2007-7-21 22:38:21 > top of Java-index,Java Essentials,New To Java...
# 24

> Anyway you must limited

> the call to a private static method or the

> initialization of an inner class to the defining

> class.

You can call static methods and static fields from non-static methods, just not the other way around!

You can call private members from ANY method in the same class, no matter if the calling method is private, public or whatever.

More clear now?

-Puce

Pucea at 2007-7-21 22:38:21 > top of Java-index,Java Essentials,New To Java...