Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!

Stephan van Hulst

Saloon Keeper
+ Follow
since Sep 20, 2010
Forum Moderator
Stephan van Hulst currently moderates these forums:
Enschede, The Netherlands
Cows and Likes
Cows
Total received
143
In last 30 days
0
Total given
138
Likes
Total received
1623
Received in last 30 days
17
Total given
168
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Stephan van Hulst

Actually, I would press you to learn default access as soon as possible. There's a good reason this access level is the default. If you need to use a member of your class outside your class, give it default access.

People get into a bad habit of making all of their non-private members public. Don't do this, only use public when you really need to.
5 minutes ago
If the initial number has nothing to do with the credit card number, and I write my credit card number as 0-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6, how should it know that it should match 1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6 and not 0-1-2-3-4-5-6-7-8-9-0-1-2-3-4-5?
17 hours ago
Welcome to CodeRanch!

It doesn't match 16 digits because the {13,16} quantifier backtracks before the *? quantifier expands. One solution is to make your lazy quantifier greedy.
17 hours ago
Enclosing classes may access private members of nested classes and vice versa. That's because all code within the body of a top-level class (including nested classes) may access private members declared anywhere within that same body (including other nested classes).

A great advantage of this is that you can declare nested classes that only expose certain properties to the outside world, and have the enclosing class manipulate their internals as needed. Otherwise the only advantage of having static nested classes at all would be namespacing.

[edit]

Ganesh beat me to the punch.
23 hours ago
Sadly, the Matcher class does not support multiple captures for the same capturing group. If you have a capturing group inside an expression with a quantifier, the matcher will only return the last match, not all matches.

As Henry said, you have to run a regex multiple times against the input to get what you want.
23 hours ago
That leaves up to interpretation what is included in the body of the top level class. I'm pretty sure that includes the bodies of nested classes though.
1 day ago
Because when you create an instance of C, it will first run the initializers and constructor of A, because C extends A. Since the constructor of A prints "A+", that will also happen for each instance of C that you create.
1 day ago
My apologies, I had considered this scenario, but I was strongly convinced that you could actually access the private members of top-level classes declared within the same file.

Ganesh Patekar wrote:Rather I would say private members are directly accessed only within the class they are declared in.


This is also not correct. A nested class can access private members of an enclosing class, and an enclosing class can access private members of a nested class.

I guess the correct way to describe private access would be: Only code within the declaring class, or code within enclosing or nested classes of the declaring class can access the private member.
1 day ago
To see what code is generated for your own serializable classes, you'll have to use a Java decompiler.

To see the reflective code that is used to read and write your serializable classes, check out the source files for the ObjectInputStream and ObjectOutputStream classes.
2 days ago
An overview:

private: Only code within the same source file can access the member.

default: Only code within the same package can access the member.

protected: Only code within the same package and code inside a subclass can access the member, but when from a subclass, you can't access the inherited member through a reference of the superclass.

public: The member is accessible everywhere. Since Java 9, modules only have access to public members in other modules if the dependency module exports the member.
2 days ago

Mickey Loveless wrote:A final variable is also called a constant variable cause it cannot be changed after initialization. if you dont provide a number at its initialization it is assigned nothing not 0.


This is not completely accurate. In some circles, "constant" refers to final variables of immutable types, because if a final field refers to a mutable object, you can still change the properties of that object.
5 days ago
That example would be better if you just called getName() in line 3, without the super.
6 days ago
Because the member is NOT inherited. A Hippo has name data, not a name member.
6 days ago
Let's make a different example. Say that the name field is public, and therefore Hippo inherits it. Hippo has a name (the data in the block of memory that represents the Hippo), AND a way to access that data through a field that is inherited from Animal. When we make that field private, Hippo still has a name, but it loses a way to access that data.
6 days ago
Yes, a Hippo does have a name. You just can't access it through an instance member called name. You can access it through an inherited instance member called getName().
6 days ago