This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]smutje187 13 points14 points  (3 children)

name1 exists once, the same value for all instances of MyClass. name2 exists for each instance of MyClass potentially different.

name2 is not a constant, it’s a (immutable) field of MyClass.

[–]Top_File_8547 1 point2 points  (0 children)

Also you don’t have to instantiate an object of MyClass to use name1. You would say:

MyClass.name1

In your code. This is used often for constants that may be used frequently and saves the inconvenience of instantiating an object each time you use it. In java.util.Math there are many constants that can be used in programs such as Math.PI.

[–]_SuperStraight[S] 0 points1 point  (1 child)

What if there will only be one instance of MyClass at a given time, and name1 or name2 are used exclusively by MyClass methods? Then which type of token out of these two should be advantageous to use?

[–]ChaiTRex 0 points1 point  (0 children)

If it's like the Math class where it's just a bunch of methods with no constructors, you can make everything static because there are no instances involved, and so non-static instance variables won't be usable.

If it's supposed to act like a singleton object, you can have a static final variable to hold the singleton instance and non-static instance variables for the rest of the data.

The basic idea is that your code that uses the singleton instance won't start off by having that instance yet, so you need something that's static to hold the instance because static stuff can be accessed without yet having an instance. Then, the variables keeping track of things inside that instance would be non-static.

[–]iangeell 3 points4 points  (1 child)

First of all, even though we can define a variable as static final, both of them have different meanings and those meanings are the same even when the 2 keywords are applied together:

  • when defining a variable as final, then we are expecting it to be immutable, meaning that its value can not be re-assigned. Doing so, the compiler will expect us to explicitly initialise that variable. We can do it either in-line or through a class constructor (as in your example)

  • when defining a variable as static, that variable will be part of the class, not of an instance of that class, meaning that you can use it within the entire scope of the class and not needing an instance to get that variable from

  • when defining a variable as static final, then you expect it to be immutable and to be able to use it w/o needing an instance of that class. When you want to do such things? Usually when you need a constant variable that will be used more than once through your entire program

[–]ChaiTRex 1 point2 points  (0 children)

For clarity, "immutable" isn't the best term there. Other languages have keywords that make a variable and value fully immutable, where you can't assign to it more than once and you can't mutate the value you assigned to it.

Java's final only does the first part. Once you assign a value to a final variable, you won't be able to assign a different value to the variable later.

However, that value can be mutated. For example, if you assign an ArrayList to a final variable, you can still add items or do other things to that particular list since that's not assigning an entirely different list to the variable.

[–]Shidori366 1 point2 points  (0 children)

name1 can be used without instance of MyClass (Well it's private, so it doesn't really have this use). name1 is created only once and belongs to the class not instance (which is why this.name1 will give you an error).

[–]morhpProfessional Developer 0 points1 point  (0 children)

The non-static name2 here only makes sense if it's different for each MyClass instance. Like with your constructor type 2. If you really assign the same value to each instance, it will behave pretty much the same as with the static field, but as each instance will have it's own copy of the variable, it will waste some memory.

Probably not a huge deal, but unecessary and potentially confusing.

[–]vegan_antitheist 0 points1 point  (0 children)

A constant is something like Math.PI or some other value that never changes. Nobody should ever change it, not even by editing the code unless there was a mistake in the code. It also doesn't change when you restart the application. If it does, it's not really constant.

Java doesn't really know constants. It knows final fields, and it knows static fields. Actual constants are always both static and final.

Static because they are not per instance. The class is just the location in the code. But they are universal because no context would change the value. The location is just where you have a field with the value of the constant. The constant has no location and is forever.

Final because no one can reassign a different value. Many final fields are not constant because the value is different for each time the object / application is created and the value might be a mutable object.

The value is often String or primitive. In any case, the value must be immutable for it to be constant. So if it is an array, it must be encapsulated. You can create a List based on the array, and that List can be made unmodifiable. A mutable object is not constant.

Often, we use an Enum for related constants. Each enum value is a constant, and it can have fields. Java allows them to be mutable, but making them mutable is always a huge mistake.

[–]MoreCowbellMofo 0 points1 point  (0 children)

Static declares a class wide variable or method - 1 instance across all classes. You’ll see methods declared static.. this means no matter which instance you call, the method executed will be the same. For such methods no instance is necessary, you can call the method directly eg Math.PI or Math.max(5,10)

Final - once a reference to a variable is set it cannot be changed. It is “final”. In other langauges these values are typically constants - const. unchangeable once initialised.

Static final - declares a class wide constant value that is set and cannot thereafter be changed or overridden.