Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!

Stephan van Hulst

Saloon Keeper
+ Follow
since Sep 20, 2010
Cologne, Germany
Cows and Likes
Cows
Total received
257
In last 30 days
3
Total given
218
Likes
Total received
2708
Received in last 30 days
26
Total given
436
Given in last 30 days
4
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephan van Hulst

I felt like rubbing your nose into it.
8 hours ago
Agree with Mike. The usual way to go about this is to just use a Map<Status, Collection<Status>>.

Can you tell us more about why a state consists of two statuses, and why you need a multimap of statuses?
8 hours ago

Campbell Ritchie wrote:...you have no way to get the generic information into the method.


Untrue. You can explicitly pass the type arguments:

or you can have the type argument inferred from the invocation context:

The second case is common, and it's what Henry is after. Java will infer the type argument and all you have to do is implement the method body like any other generic method, whether it takes method parameters or not.
8 hours ago

Junilu Lacar wrote:In all other cases, most people will assume that you're likely referring to a custom/user-defined class.


Altough here too, the word "user" is ambiguous. I would avoid it here and only use it when you're referring to the person who is interacting with the running application.
9 hours ago
Henry, your original approach was just fine, except you can use the diamond operator.
10 hours ago
You can ignore Spring for most of this. If you want to store the machine's current state in a database, just write it on top of an interface that is responsible for storing and retrieving the current state and let Spring inject it.

I don't really know Kotlin, but in Java I would write something like this:
12 hours ago

bryant rob wrote:I will just state that I created an object named theCase of type Case which is a user defined variable,  and leave it at that....I hope I have it right now.


No. objects aren't named, unless you give them a field called "name".

What you did was create an instance (or object) of actual type Case and you assigned it to a local variable with formal type Case. The name of the variable is "theCase", not the name of the object.

"Defining" a variable means giving it a value, but it's much more common to just say "initialize" or "assign a value to". The word "defining" is much more commonly used to refer to to writing the body of a class, or the body of a method.

You meant "User declared variable", but that also doesn't mean much. Who is the user? The author of the class? Well, isn't the author of the class always the one who declares the variables in the class?
12 hours ago
I don't completely agree with Campbell (there are all kinds of reasons you might want to have a generic method infer its type arguments using a variable that the return value is assigned to), but his skepticism should give you pause.

In general such methods are only used as factory methods for empty instances of container types. Collections.emptyList(), Steam.empty() and Optional.empty() are good examples, but non-containers also have valid examples, such as Comparator.naturalOrder() and Comparator.reverseOrder().

Why can your Node class be empty? How does it differ from the lack of a node, which you could express using Optional<Node<T>>?

By the way, you generally can solve your problem more easily by actually *using* type inference:

Kudos on using a named factory method instead of a parameterless constructor though. Just make the constructor private and name the method something better, such as emptyNode().
13 hours ago
I don't think there's really a standard guide for member ordering.

For personal projects, I prefer this order:

  • static fields
  • fields
  • simple nested classes/interfaces/enums
  • constructors
  • static factory methods
  • simple properties (getters/setters)
  • calculated properties
  • other instance methods
  • other static methods
  • other nested (private) classes

  • I don't follow this list religiously though. What works well for one class may not work for another.

    What's important is to be consistent, but not draconian.

    And as always, if you're working for a company that prescribes One True Way of doing things, you suck it up and do what they tell you to.
    3 days ago
    You can call Thread.CurrentThread to see which thread is currently running.

    Note that in an async method, every time you use the await keyword, the code that comes after is potentially run by a different thread than the code that comes before the await. The runtime will try to get the original thread to execute the code, but I don't think it's guaranteed.
    3 days ago
    It's great that you are driven. You'll get there eventually.

    I have some comments on the design of the Point class. It's not about what you CAN do, it's about what you SHOULD do.

    Your Point class contains setters for the x and y fields. While setters are unavoidable in some cases, for many types they are actually harmful.

    When you think about the points (2, 3) and (4, -1) in 2D coordinate space, you think of them as two fixed objects, right? "Setting" the x coordinate of (2, 3) to 1 doesn't really make sense. Instead, you just think about a new point with coordinates (1, 3).

    The reason it's harmful is because you might pass an instance of type Point to another object that assumes that the point will never change. Then when you do change the coordinates of the point, the object that depends on the Point will have been corrupted.

    Secondly, use names that are as descriptive as possible. A distance() method without parameters didn't make sense to me until I looked at the implementation. Methods should be clear in their purpose without having to look at their implementation.

    Override the toString() method if you can to give your objects a good textual representation for debugging. Override the hashCode() and equals() methods if your class represents a value type: Objects of a value type represent a fixed value that can be compared to another object of the same type, such as the number 7.3, the character 'K' or the point (4, 2). Your Point class is an ideal candidate if you drop the setters.

    Make your classes and members as private as possible. Lose the habit of writing public everywhere. Only raise the visibility of a class or method if you really need it outside of the package or class you declared it in.

    Make your classes final. This prevents them from being subclassed. Subclassing is a powerful tool, but it comes with a lot of unforeseen challenges that are hard to deal with if you haven't carefully designed your class. The easiest thing is to just prevent it.

    Here's an updated version of your class.
    3 days ago
    Building your own requests by hand is not a good idea, especially not if they are complicated, as SOAP tends to be.

    You should be using a JAX-WS implementation to send SOAP requests. I've had good experiences with Glassfish Metro.

    If you come across it, avoid Axis2 like the plague. It's a terrible framework that will end up costing you more than it's worth.

    bryant rob wrote:secondPoint is a class variable of type Class


    No. secondPoint is just a variable, not a "class variable". Java has a different concept that your phrasing could be confused with, but you're not there yet. Also, the variable is not of type Class. It is of type Point, and point is a class.

    So, secondPoint can only reference objects that I create that are instances of a Point....and in my case actual points represented by (x,y) coordinates. (i.e. Point 1(3,5) Point 2(8,6) Point(5,3) and so on).


    Yes.

    Meaning I can't actually state somewhere in my code:

    Point thirdPoint = new Point();

    Point anyPoint = thirdPoint;


    Why wouldn't you be able to do that?

    Lastly, could you please provide me an example of how I could do this with a subtype of Point?


    Point is a bit of a bad example to do this. It doesn't make a lot of sense to subclass a point in 2D space.

    Are your reading the Java tutorials? You'll run into subclassing and inheritance soon enough.
    4 days ago
    Welcome to CodeRanch!

    I think that conceptually, you understand what's going on just fine, you're just using the wrong terminology.

    Point is not a class variable. It's a class. secondPoint is a variable of type Point, meaning it can hold references to instances of type Point.

    You can not assign just anything to secondPoint. Only references to Point objects or references to objects that are a subtype of Point. You can not assign a Triangle to a variable of type Point.

    I don't really understand your question "What is it about the class type variable that makes this code work?". What alternative did you think would work that didn't?
    4 days ago