0

I have the following code:

private static double calcTotalMass() { return bike_mass + human_mass; }
private static double calcWeight() { return calcTotalMass() * grav_acc; }
private static double calcWork() { return calcWeight() * height; }
...

Since in Java everything is in a class and I can't define global functions, static and an access specifier is necessary.

In C++ I could shorten the first function to just

double calcTotalMass() { return bike_mass + human_mass; }

In Julia it'd be

calcTotalMass() = bike_mass + human_mass

but that's a different story.

In C++ access specifiers are in groups that way the private does not have to be repeated for each method. Is there something similar in Java or is this just how the language is built?

Post Self
  • 1,471
  • 2
  • 14
  • 34
  • 5
    You need to define `private` everywhere you need it. – achAmháin Oct 04 '18 at 20:52
  • 3
    short answer is no, I believe you need to put the private keyword to a method if you want its access to be within the class level – wenzi Oct 04 '18 at 20:54
  • 1
    Do these extra keywords actually really matter? How many of them are you writing that you really need to care about adding "private static" a few times? – Andy Turner Oct 04 '18 at 21:28

1 Answers1

4

No, each member has its access specified individually.

However

  • You could put the methods in a nested class/interface and declare the nested type private. Unfortunately the code lurches to the right, which is never good. Also, IIRC, there's something strange preventing an import static from your own nested class.
  • If you wanted to bother testing the methods, private is a really bad choice. Better put them in a separate "package private" class or interface.
  • The default access modifier for class members is "package private" which may be acceptable.
  • The default access modifier for interface members is public which is useful. For member variables you can also junk the static.

There is also a JEP, Concise Method Bodies, that may make your code slightly shorter and more like your Julia example.

(Whilst I'm here: "calc" in a method name is usually redundant. Mutable statics are a really, really bad idea.)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Well, I am testing the methods. I am currently only using a single class (i.e. no class) so my testing code is just in a `public void` method. Is JEP already a thing? Since it says draft on top of the document. That syntax would be awesome. Good to know that I can just leave off the access specifier (makes no difference in my current code). – Post Self Oct 04 '18 at 21:10
  • Yes, I know the code is bad, but that's my quick uni assignment, it's easiest with just some global variables since the calculation happens only once with values from stdin :) – Post Self Oct 04 '18 at 21:11
  • What do you actually mean with _mutable statics_? – Post Self Oct 04 '18 at 21:12
  • @PostSelf Mutable static (a.k.a. global state): A static field that can be modified itself, or directly or indirectly references an instance field that can be modified. – Tom Hawtin - tackline Oct 04 '18 at 21:15
  • Yes, I see. So the analog to a global variable. That's what I thought – Post Self Oct 04 '18 at 21:17