I'm coming to Java from Python and thought that this is basically like Python's self...but this small code confuses me. Functionally, this code:
public class Test {
private String name;
public Test(String givenName)
{
this.name = givenName;
}
public String nameGet()
{
return this.name;
}
public static void main(String[] args)
{
Test example = new Test("Hello Guys");
System.out.println(example.nameGet());
}
}
does the same exact thing as this code:
public class Test {
private String name;
public Test(String givenName)
{
name = givenName;
}
public String nameGet()
{
return name;
}
public static void main(String[] args)
{
Test example = new Test("Hello Guys");
System.out.println(example.nameGet());
}
}
Since this, pardon the pun, seems to be the case, what then is the point of referring to this when working within the class?
public Test(String givenName)
{
this.name = givenName;
}
The this. is not needed in this case or in the get method). It is commonly used when the code is like this instead:
public Test(String name)
{
this.name = name;
}
Which tells the compiler to set the instance variable (this.name) to the local variable (name).
Some people do it to be very clear that they are using an instance variable.
It's often not needed but may be necessary in case of ambiguity.
Say your constructor parameter was called name then there would be no way of determining which variable you're referring to.
Thus you would have to use:
public class Test {
private String name;
public Test(String name) {
this.name = name;
}
}
(On a side note; if you'll ever work with inner classes and you've got name ambiguity you use OuterClass.this:
public class Test {
private String name;
private class InnerTest {
InnerTest(String name) {
Test.this.name = name;
}
}
public Test(String name) {
new InnerTest(name);
}
}
Related
I am trying to figure out how, if possible, to copy one object into another. It is not as simple as I mean because I am trying to copy objects from class B into an object from class A.
I will try to show examples of code.
public class A {
ArrayList<String> areaList = new ArrayList<>
}
Then I create that actual area in the next class, or maybe I should be creating it in the first class.
public class B {
A area = new A[10];
}
And here is the next class:
public class C {
B teachers = new B[5];
}
I want to create multiple different areas with multiple different teachers. In area 1 lets say its math, I want 4 different teachers. I am looking for just name, number, and salary of the teachers so 3 attributes. In area 2, lets say its English, I would like to enter 5 English teachers. But I still want to be able to reference my first area which would be area[0] and see the original 4 teachers I inserted in that list after I add the English teachers to area[1]. I wasn't sure how to do that or how to even approach that because when I try to set them equal, it compiles an error because they are from different classes technically. I want to see if I can do this without extending class.
I think you mean something like this, hm? If you add one teacher in two different areas it will be the same teacher because you will add a reference to the list.
public class MainClass {
public static void main(String[] args) {
ArrayList<Area> areasList = new ArrayList<>(10);
Area math = new Area("math");
areasList.add(math);
math.addTeacher(new Teacher("Mr. Freeman", 3500f));
math.addTeacher(new Teacher("Mr. Greedy", 3200f));
}
}
class Area {
private String name;
private List<Teacher> teachersList;
public Area(String name) {
this.name = name;
this.teachersList = new ArrayList<Teacher>(10); //define what type of list do you want
}
public String getName() {
return name;
}
public List<Teacher> getTeachersList() {
return teachersList;
}
public void setName(String name) {
this.name = name;
}
public void addTeacher(Teacher teacher) {
this.teachersList.add(teacher);
}
public void removeTeacher(Teacher teacher) {
this.teachersList.remove(teacher);
}
}
class Teacher {
String name;
float salary;
public Teacher(String name, float salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
I have an existing class, that I want to keep ignorant of javafx. Is there a generally accepted way to decorate or adapt it so that it supports javafx properties?
I want to do something like the following (which is obviously wrong):
public class FooWrapper {
/**
* Decorates a Foo instance with javafx stuff
*/
private final Foo foo;
public FooWrapper(Foo toWrap) {
this.foo = toWrap;
}
private final StringProperty name = new SimpleStringProperty();
public final StringProperty nameProperty() {
return this.foo.getName();//?????
}
public final String getName() {
return this.nameProperty().get();
}
public final void setFirstName(final String name) {
this.nameProperty().set(name);
}
}
public class Foo {
/**
* Basic class I want to keep ignorant of javafx
*/
private String name = "hello";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Use the classes in the javafx.beans.property.adapter package.
public class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class FooAdapter {
private final JavaBeanStringProperty name;
public FooAdapter(Foo foo) {
try {
name = JavaBeanStringPropertyBuilder.create().bean(foo).name("name").build();
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
}
public final void setName(String name) {
this.name.set(name);
}
public final String getName() {
return name.get();
}
public final StringProperty nameProperty() {
return name;
}
}
The adapter property, as created above, requires that the underlying object follows the Java Bean convention for properties. However, there are ways to customize what methods to use for getters/setters.
The adapter property will get the value from the underlying object and, if writable, also write to the underlying object when updated. It can also observe the underlying object for changes if it supports PropertyChangeListeners. Note that this functionality is implemented using reflection; if you are using modules you need to add the appropriate exports/opens directives to your module-info (see the javadoc of the various properties, such as JavaBeanStringProperty, for details).
I got the following class:
public class Possibility {
private String name;
public Possibility(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
If I now have many classes that extend "Possibility", how can I find how many instances exist of classes that extend Possibility?
You can use a static field as a counter in Possibility class and use it to increment as the objects are created. This is more efficient and secure than using reflection.
package so;
public class Possibility {
private static int counter = 0;
private String name;
public Possibility(String name) {
counter += 1;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Possibility1 p1 = new Possibility1("p1");
Possibility2 p2 = new Possibility2("p2");
System.out.println(Possibility.counter);
}
}
Possibility1
package so;
public class Possibility1 extends Possibility {
public Possibility1(String name) {
super(name);
}
}
Possibility2:
package so;
public class Possibility2 extends Possibility {
public Possibility2(String name) {
super(name);
}
}
Possibility3
package so;
public class Possibility3 extends Possibility {
public Possibility3(String name) {
super(name);
}
}
The Reflections library provides a pretty easy way to do this:
int numSubTypes = reflections.getSubTypesOf(Possibility.class).size();
you must create an integer attribute in Possibility class and and you can get this integer from another class that extends from Possibility, like this:
class Possibility{
public int someInteger;
//getter
public int getSomeInteger(){
return this.someInteger;
}
}
class someClass extends Possibility{
public void someMethode(){
Possibility possibility = new Possibility("someName");
//get someInteger
possibility.getSomeInteger();
}
}
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?
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
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());
}
}
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";
}
}
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.
I have researched, and although this is a really simple issue, I am not sure how to solve it.
The code I have looks like this:
public class Playlist {
public Playlist(String name) {
}
}
Separate files of course:
#Test
public void CreatePlaylist(){
Playlist myPlaylist = new Playlist("Workout Playlist");
}
I am trying to print the actual name of this new playlist "workout playlist" but I can't seem to find a way to do so.
You need to store the name of your playlist in an instance variable. For instance:
public class Playlist {
private final String name;
public Playlist(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Then you can print it with:
System.out.println(myPlayList.getName());
If you want to make the name mutable, then get rid of the final modifier and add a setName(String) method.
write get method to name or override toString method in the class
public class Playlist {
private String name;
public Playlist (String name) {
this.name = name;
}
public String getName() {
return name;
}
#Override
public String toString() {
return "Playlist [name=" + name + "]";
}
}
Print the name using
System.out.println(playlistObject.getName());
or
System.out.println(playlistObject).
I would prefer setting a getter method over toString() though.
public class Playlist {
private String name;
public Playlist(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Then to show the name:
#Test
public void CreatePlaylist(){
Playlist myPlaylist = new Playlist("Workout Playlist");
System.out.println(myPlaylist.getName());
}
You are not at all storing the 'name' property in your object. So obviously you can't access name. One way is
public class Playlist {
public String name;
public Playlist(String name) {
this.name = name;
}
}
Now you should be able to access your attribute from your testcase like this.
#Test
public void CreatePlaylist(){
Playlist myPlaylist = new Playlist("Workout Playlist");
System.out.println(myPlaylist.name);
}