This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 8 years ago.
It isn't by default like that?
For example:
**public** int [name];
**public** String [name];
By default it's package-private access (allowing any class belonging to the same package to access it), not public.
Anyway, it's bad practice to define data members as public. In most cases they should be private.
Related
This question already has answers here:
Why instantiation of static nested class object is allowed?
(3 answers)
Can a static nested class be instantiated in Java?
(5 answers)
What does the 'static' keyword do in a class?
(22 answers)
Static Classes In Java
(14 answers)
Closed 3 years ago.
I know that java doesn't support static Top-Level classes, and only nestled classes can be static.
But, wherever I read about these nestled static classes, it says you have to initialize these classes with the "new" keyword.
Is this not instancing? if yes, how are we instancing "static" classes?
Does this mean we can have multiple instances of this class running simultaneously? Can we store these on variables just like non-static classes?
If this is true, then is the "static" modifier just a modifier used to access the nestled class without instantiating the container class?
Any Light shed on these questions are very welcome. Thanks!
EDIT: This question is different than the questions it has been marked duplicate against as I am NOT asking what the static modifier does to an object, nor am I asking if static classes exist. I know what static nestled classes are, and how static objects behave, but the static nestled class'es peculiar behaviour of being instantiatable by using the"new" keyword (which is used to create an instance of an object). Static classes can usually not be created instances of in OOP, and I was asking is this different in java. can you create instances of static classes in java.
This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 5 years ago.
I know this might be thought of as a duplicate question, but I think my question is a bit different from the previous questions.
The difference between public and protected members is that a public member acts as a protected member on in that it can be accessed from the world but a protected member cannot.
What does the term world mean? Does it mean from outside the class? If so how?
Is it by using the class name? Or they can be accessed by using the variable/method name directly without the current class being a subclass?
Okay, the word "world" means that it can be accessed from anywhere inside the project (no matter if they are in the same package). However the protected members means that they can only be accessed from other classes inside the same package. Sources
This question already has answers here:
What does "Can be package local" mean? (IDEA Inspection)
(5 answers)
Closed 6 years ago.
In IDE Some public methods are marked with a warning as Access can be package local and if I remove public form the method no warning will be shown.
Is it a good practice to do so? Should I keep them public anyways ?
If the method is only accessed within the package, the best practice is to use the "default" access modifier (by not specifying any access modifier). Further more briefly here are the four access modifiers used in Java and there accessibility levels.
default- Visible to the package. No modifiers are needed.
private- Visible to the class only.
public- Visible to the world.
protected- Visible to the package and all subclasses.
This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 7 years ago.
I am new to java. Can anyone please tell me best way to access private method variables in another class. Thank u
Private variables are private for a reason- you're not supposed to be able to access them directly. Many classes do have getter methods though which allow you to access private variables but not change them. If you need to access private variables in your program, you need to rethink your design.
This question already has answers here:
How do I test a class that has private methods, fields or inner classes?
(58 answers)
Closed 8 years ago.
I have some abstract class and some classes that inheritance from it.
I want to make JUNIT tester for it. I tried use reflect on the sub classes but in this way I cant see the private variables in the absract class.
how can I get to, Or maybe I can create instance of the abstract class for this?
To see all fields of a class use Class.getDeclaredFields()