2

I need to write some code which is as follows:

public class Person { public static final String NAME; public Person(String NAME) { this.NAME = NAME; } } 

public class Player extends Person { public Peter(String name) { super(name); } } 

It's basically, I want the Player class to have a static final field called NAME, that is being initialized somewhere else, without manually writing in every class public static final String NAME = "Peter".

Is it possible?

10
  • 3
    "why can't i just write "need help with" in the title?" Because it's a vague, poor title indicative of a potentially vague, poor question. Please come up with a title that actually describes your question. As it is now, your title is not useful. Commented Sep 13, 2015 at 21:00
  • You can't change final field value somewhere else without reflection. Commented Sep 13, 2015 at 21:03
  • What do you mean, without manually writing in every class "public static final String NAME = "Peter"? You just said you want this field just in the Player class, so you'd only have to write that code once. Commented Sep 13, 2015 at 21:06
  • Your design doesn't seem to make sense in the real world. A static field is shared between all instances. Do all your people share their name (have the same name)? Commented Sep 13, 2015 at 21:06
  • Why do you have to write public static final String NAME = "Peter"; in every class? You can just have the field in one class and use it in others. Commented Sep 13, 2015 at 21:07

4 Answers 4

4

As it has been said in the comments, you have poorly declared your NAME variable. In actuality, you don't want it to be static (although you can keep the final modifier, if you want). Your code should, instead, be something along the lines of:

public class Person { public final String name; public Person(String name) { this.name = name; } } public class Player extends Person { public Player(String name) { super(name); } } 

Every person should have their own name; you don't want all objects to be sharing one NAME field

Sign up to request clarification or add additional context in comments.

Comments

1

I do not know if I fully understand your question, but I think you have a few mistakes in your code. Like declare name of person as static variable, because static variables are often used as variables for the entire class, and if you changed the name, would change the name to the entire class, not for one instance. Also final is wrong, because you cannot set final variable.

I would do something like this:

public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return String.format("Person: %s", this.getName()); } } public class Player extends Person{ public Player(String name) { super(name); } public String toString(){ return String.format("Player: %s", this.getName()); } } public class Match { private Player player_one; private Player player_two; public Match(Player player_one, Player player_two) { this.player_one = player_one; this.player_two = player_two; } public Player getPlayer_one() { return player_one; } public void setPlayer_one(Player player_one) { this.player_one = player_one; } public Player getPlayer_two() { return player_two; } public void setPlayer_two(Player player_two) { this.player_two = player_two; } @Override public String toString() { return String.format("Right now are playing %s VS %s",player_one.getName(), player_two.getName()); } } public class PlayerTest { public static void main(String[] args) { Player peter = new Player("Peter"); Player anna = new Player("Anna"); Match tennisMatch = new Match(peter, anna); System.out.println(tennisMatch.toString()); } } 

Comments

0

I static field (variable) only exists once for all instances of your class. Therefore what you try does not work by design.

What value would you expect the field to have after you created three different instances of this class using different parameters?

A final variable cannot be changed once it got initialized. For static variables this happens before the first instance of the class is even constructed. At the moment the constructor is executed the field cannot be changed anymore.

To initialize a static final variable you have to assign a value directly at the definition using the = operator or you have to do it in a static initializer which looks like this:

public class FooBar { public static final String STATIC_VARIABLE; static { STATIC_VARIABLE = "Hello World"; } } 

Comments

-2

You can make it like this:

private static final NAME; public Player(String name){ NAME = name; } 

A final varible can be initialized once only if it wasn't initialized yet. So in this way the constructor is helping you make it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.