I am a noob in Java and am someone who is learning Java after python.
Anyways, I am having a hard time figuring this out.
Suppose I have the class
class Bicycle{
....
}
and
public class Bicycle{
....}
what is the difference.
And what about
public static class Bicycle{
// if this can be a valid class def in first place
}
and then, after this.. lets talk about variables.
class Bicycle{
int Gear or public int Gear // whats the difference
}
When to use which one?
These keywords (or lack of them) are known as access modifiers – in short they control the accessibility of classes or members.
Classes have the following modifiers:
public– accessible anywhereClass members have more possibilities:
public– accessible anywhereprotected– only accessible in same package or in an extending classprivate– only accessible in same class file**Note that nested classes can access their outer class’s
privatemembers and vice-versa.More information on access modifiers can be found here. Also see this helpful article for the basics.
Edit: I missed your middle example, with
public static class Bicycle– thestatichere must mean thatBicycleis a nested class. See this page (which I had already linked in my subscript) for an explanation of nested classes, which break down into static classes and non-static, aka inner, classes.