-3

I know that in JavaScript, you don't need the public keyword in the following code :

class myClass
{
  public int myVariable;
  // it is the same as :
  int myVariable
}

do you need it in Java ? What is its purpose ?

2Dee
  • 8,609
  • 7
  • 42
  • 53
mobilGelistirici
  • 449
  • 4
  • 7
  • 19
  • 2
    Look up this simple explanation of prefixes: http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – Trompetas Rouketas Nov 14 '13 at 15:15
  • 2
    Come one guys. This question has been asked and answered a dozen times here on SO already. Do some research and stop the SPAM. – mwhs Nov 14 '13 at 15:18
  • @mwhs when someone new posts to SO I like to give them a short answer (like mine) it contains enough to get them searching in the right direction, but isn't so long it wastes my time. The rest are just vultures circling the easy questions. :P – Alec Teal Nov 14 '13 at 15:25
  • @AlecTeal you should vote the vultures down to protect against inapropriate dilution of your reputation. – mwhs Nov 14 '13 at 15:27
  • Hey, a brotha has got to eat. Besides, @mwhs needs to read [what constitutes a downvote](http://stackoverflow.com/help/privileges/vote-down) according to the community and not make up his own rules. – Vidya Nov 14 '13 at 15:27
  • @Vidya "Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." --> nuff said. – mwhs Nov 14 '13 at 15:28
  • Why would you compare Java to Javascript? Java is to Javascript as Car is to Carpet. – GriffeyDog Nov 14 '13 at 15:29
  • @mwhs I've lernt that reputation is pretty meaningless, so I don't donwvote correct vultures. To prove my case http://stackoverflow.com/questions/18543980/symbol-not-found-when-using-template-defined-in-a-library/18544093#18544093 is a gem of an answer, it is not short. That is why I come to SO, so searchers may find that and stuff. – Alec Teal Nov 14 '13 at 15:30
  • 1
    @mwhs, while I congratulate you for being able to cut-and-paste, you are welcome to downvote the question. But why downvote every answer like a firing squad? And did you actually just say "nuff"? That stopped being cool like 20 years ago. Will you hit us with "You can't handle the truth" next? – Vidya Nov 14 '13 at 15:32
  • @Vidya You as a technology and code lover should definitely be much more sensible about redundancy reduction. Had any answer contained a reference to another SO question about the exact same topic, fine. Blind and context-free upvoting of an answer just because it is correct is probably the other side of the coin when working with this reputation points system. Fortunate enough there is actually the possibility to also downvote an answer, when people are obvioulsy only chasing for points instead of really contributing to the sanity of the overall project. – mwhs Nov 14 '13 at 15:40
  • What you call "chasing for points' is people eager to help a beginner. I think that's a good thing. Second, this question had more than `public`. The OP says something wrong about JavaScript. His class naming convention is off. Third, despite being "nuff said" (I still laugh at that), you made up your own rules for downvoting answers. To use terms you might understand, "Why you trippin"? Maybe people ignore you in real life, but SO isn't the place to work out your influence issues. – Vidya Nov 14 '13 at 15:48
  • @Vidya Why are you so mad? What is your point? I have criticized the question for its redundancy and low quality and also downvoted the answers that didn't link to questions on the same topic here on SO. How can there be a correct answer to a "wrong" question? BTW: the question has been put on hold, I was certainly not the only one to think it was bad/off-topic/etc, but maybe the only one to express my opinion. You on the other hand are trying to insult and provoke me. – mwhs Nov 14 '13 at 16:04
  • @mwhs, don't confuse sarcastic mockery with anger. You were wrong to downvote the answers since they didn't meet the community standard for downvoting. Your personal standards are irrelevant. Also, you clearly aren't smart enough to distinguish between downvoting *questions* and downvoting *answers*. Yes, people agreed your question downvote was right. That's fine. **No one** agreed with your answer downvotes. Stop conflating the two. I just think it is hilarious when trolls ignored in real life feel compelled to flex their muscle online. – Vidya Nov 14 '13 at 16:24
  • Maybe you should review your defintion of sarcasm. Stop being impolite please. – mwhs Nov 14 '13 at 16:46

3 Answers3

3

Yes if you want something to be accessible everywhere.

Otherwise it is package-visibility, meaning only stuff in the same package (at some level) can access it.

Alec Teal
  • 5,770
  • 3
  • 23
  • 50
  • 3
    Loving the unexplained downvote. – Alec Teal Nov 14 '13 at 15:19
  • Got one today as well, there must be a downvote troll lurking around :) – 2Dee Nov 14 '13 at 15:25
  • @2Dee I get that it isn't detailed, but it's not supposed to be, this question can be solved by searching not even just SO. I like to give an answer to such questions to new users because it points them in the right direction but without wasting time. I hope others practice this. – Alec Teal Nov 14 '13 at 15:27
  • Fully agree... You, sir, deserve an upvote for good spirit ! – 2Dee Nov 14 '13 at 15:29
  • @2Dee in the hope of converting someone to my way (answering duplicates if only briefly then usually moving on - this is an exception) when a good question comes along http://stackoverflow.com/questions/18543980/symbol-not-found-when-using-template-defined-in-a-library/18544093#18544093 - I'm not a vulture circling for the easy ones! You can tell an easy one because it goes from 0-4 answers within 4 seconds after the question has been up for 2 minutes :P – Alec Teal Nov 14 '13 at 15:36
  • First of all, distinguishing the "easy" ones from others is silly because the only view that matters is that of the OP. And helping him or her is just as valuable as helping someone who wants to build a time machine in Cobol. Second, we need to follow community standards rather than impose what we think is a good question or answer. Do that on your own site. Bottom line, it's less about you and more about the OP and the community. Humility, people. – Vidya Nov 14 '13 at 15:40
  • @Vidya many people can multiply numbers, much fewer are dragged through analysis. To someone enduring analysis multiplication questions are easy. – Alec Teal Nov 14 '13 at 15:44
  • Someone just downvoted every single answer here. Troll alert! – NickJ Nov 14 '13 at 16:06
2

You don't 'need' the 'public' keyword - if you don't specify the access level of a Class variable it will be set to package-private.

More details are here - http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

TR1
  • 323
  • 1
  • 9
0

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

public fields are a bad idea, however (A VERY bad idea in a multi-threaded application). It allows other classes to change the state of your class without any control, and could break invariants. The proper way is to control state-changing through public setter methods.

NickJ
  • 9,380
  • 9
  • 51
  • 74
  • Would the downvoter kindly leave a comment as to why? – NickJ Nov 14 '13 at 15:32
  • linking/flagging would have been the correct answer to this question. – mwhs Nov 14 '13 at 15:47
  • And this somehow makes my answer invalid? I KNOW it is correct! – NickJ Nov 14 '13 at 16:06
  • Hi Nick, invalid is probably the right term, yes. While it may be correct from a context-insensitive perspective the overall rating/grading of an answer should be context-sensitive, otherwise SO will fail in the long term. There is absolutely no value to answer the same kind of question with the same kind of answers again and again. This is why I downvoted your answer. – mwhs Nov 14 '13 at 16:13