I studied the constructor (java)

I wanted to study constructors, so I googled (googled: "search on Google", even if it's not Google) and pulled the code.

ctest7.java


class ctest7{
  public static void main(String args[]){
    Television tv1 = new Television();

    tv1.dispChannel();
  }
}

class Television{
  private int channelNo;
  private String housouKyoku;

  public void setChannel(int newChannelNo){
    channelNo = newChannelNo;
    if (channelNo == 1){
      housouKyoku = "FujiTV";
    }else if (channelNo == 3){
      housouKyoku = "NHK";
    }
  }

  public void dispChannel(){
    System.out.println("The current channel is" + housouKyoku + "is");
  }
}

When I compile and run this, "Current channel is null" Is displayed. housouKyoku is not stored and is null.

It seems that if you fix this, you will get the following code.

java.ctest8.java


class ctest8{
  public static void main(String args[]){
    Television tv1 = new Television();

    tv1.dispChannel();
  }
}

class Television{
  private int channelNo;
  private String housouKyoku;

  Television(){
    channelNo = 1;
    housouKyoku = "FujiTV";
  }

  public void setChannel(int newChannelNo){
    channelNo = newChannelNo;
    if (channelNo == 1){
      housouKyoku = "FujiTV";
    }else if (channelNo == 3){
      housouKyoku = "NHK";
    }
  }

  public void dispChannel(){
    System.out.println("The current channel is" + housouKyoku + "is");
  }
}

In the original ctest7.java code

  Television(){
    channelNo = 1;
    housouKyoku = "FujiTV";
  }

Is added.

Well, that's what initialization is. There is a theory, but if there is no content as the initial value, the operation will not work. That's why you need to set the initial value. You could write it directly in the method and overwrite it each time you call it, but that's not suitable for sharing large amounts of code.

Hmm.

[reference] "What is a constructor-Constructor-Introduction to Java-Let's programming" https://www.javadrive.jp/start/constructor/index1.html

Recommended Posts

I studied the constructor (java)
[Java] I implemented the combination.
java (constructor)
I tried the Java framework "Quarkus"
I tried the new era in Java
Studying how to use the constructor (java)
[day: 5] I summarized the basics of Java
[Java] I personally summarized the basic grammar.
I went to the Java Women's Club # 1
I studied the State pattern and the Strategy pattern
Understand java constructor
I studied for 3 weeks and passed Java Bronze
Why Java was the target language I hate
I touched on the new features of Java 15
Try the free version of Progate [Java I]
I first touched Java ②
I first touched Java ③
I first touched Java ④
Studying Java 8 (see constructor)
I first touched Java
JAVA constructor call processing
[Java] How to omit the private constructor in Lombok
[Java] I want to calculate the difference from the date
I couldn't run it after upgrading the Java version
I summarized the types and basics of Java exceptions
I tried to implement the Euclidean algorithm in Java
What I researched about Java 8
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
I investigated the enclosing instance.
I started Java Gold (Chapter 1-1)
I didn't understand the behavior of Java Scanner and .nextLine ().
What I researched about Java 6
[Java] ArrayList → Should I specify the size in array conversion?
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I summarized the collection framework.
What I researched about Java 9
The Java Primer passed the blockage
I took Java SE8 Gold.
I want to use the Java 8 DateTime API slowly (now)
Mock the constructor with PowerMock
I tried the Docker tutorial!
I tried the VueJS tutorial!
What I researched about Java 7
I tried using Java REPL
I tried the FizzBuzz problem
Java concurrency I don't understand
I tried using the CameraX library with Android Java Fragment
Java is the 5th day
I tried metaprogramming in Java
[Java] I thought about the merits and uses of "interface"
I want to simplify the conditional if-else statement in Java
What I researched about Java 5
Input to the Java console
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
Java14 came out, so I tried record for the time being
I want to return to the previous screen with kotlin and java!
I passed the Java test level 2 so I will leave a note
I tried the input / output type of Java Lambda ~ Map edition ~
I tried to translate the error message when executing Eclipse (Java)
I tried to summarize the methods of Java String and StringBuilder