All Questions
Tagged with visibility java
451
questions
107
votes
4
answers
74k
views
Private constructor in Kotlin
In Java it's possible to hide a class' main constructor by making it private and then accessing it via a public static method inside that class:
public final class Foo {
/* Public static method */...
82
votes
3
answers
11k
views
Why is there no sub-class visibility modifier in Java?
On more than one occasion I have found myself desiring a variable visibility that is not possible in Java. I wanted certain members to be visible within their own class and within any sub-classes, ...
52
votes
3
answers
6k
views
Why can nested child classes access private members of their parent class, but grandchildren cannot?
Probably similar to the question, Why can outer Java classes access inner class private members? or Access to superclass private fields using the super keyword in a subclass .
But there are some ...
51
votes
9
answers
24k
views
public methods in package-private classes
Does it make a difference to mark methods as public in package-private classes?
class SomePackagePrivateClass
{
void foo(); // package private method
public void bar(); // public ...
45
votes
12
answers
16k
views
Why can attributes in Java be public?
As everybody knows, Java follows the paradigms of object orientation, where data encapsulation says, that fields (attributes) of an object should be hidden for the outer world and only accessed via ...
45
votes
6
answers
17k
views
Understanding Java's protected modifier
I have a class called A in package1 and another class called C in package2. Class C extends class A.
A has an instance variable which is declared like this:
protected int protectedInt = 1;
Here is ...
45
votes
1
answer
2k
views
Type-parameterized field of a generic class becomes invisible after upgrading to Java 7
Now Eclipse Indigo SR1 with builtin Java 7 support is finally out since a week or two, I'm migrating my playground projects from Helios SR2 + JDK 1.6_23 to Indigo SR1 + JDK 1.7.0. After a full rebuild ...
34
votes
7
answers
8k
views
How to demonstrate java multithreading visibility problems?
If variables in Java are accessed from multiple threads, one must ensure that they are safely published. This usually means using synchronizedor volatile.
I have got the impression, that some of my ...
30
votes
2
answers
11k
views
Do I need volatile for variables of reference types, too?
We often use volatile to ensure that a condition variable can be visible to every Thread.
I see the volatile fields are all primitive type in code so far.
Does object field has this problem? For ...
27
votes
3
answers
949
views
Why is the data array in java.util.ArrayList package-private?
In the java.util.ArrayList class, the object array for the list's elements is defined as package-private:
transient Object[] elementData; // non-private to simplify nested class access
The comment ...
25
votes
3
answers
21k
views
Why can I override a protected method with public method?
The Java compiler doesn't complain when I override a protected method with a public method. What's really happening here? Is it overriding or hiding the parent method since the parent method has lower ...
23
votes
3
answers
1k
views
Does SecurityContext#setAuthentication guaranties visibility?
I use spring security in my project.
I have feature to change login. To achieve this aim I use following code
Authentication authentication = ...
SecurityContextHolder.getContext().setAuthentication(...
22
votes
7
answers
37k
views
Aren't Java constructors public by default? [duplicate]
I have two classes in two different packages. For one class I've defined a constructor without setting access modifier for it. I want to instantiate an object of this class in another package and get ...
20
votes
12
answers
10k
views
the use of private keyword
I am new to programming. I am learning Java now, there is something I am not really sure, that the use of private. Why programmer set the variable as private then write , getter and setter to access ...
18
votes
3
answers
3k
views
Why can we reduce visibility of a property in extended class?
I have two classes, Parent:
public class Parent {
public String a = "asd";
public void method() {
}
}
And Child:
public class Child extends Parent{
private String a = "12";
...