java setter getter error resolution

table of contents

--A story that I was addicted to for a week due to a compilation error of setter and getter --Prerequisite environment --Grammar --Function --Specific operation details --Error this time --Cause --Reference book, HP

A story I was addicted to for a week due to a getter and setter compilation error

That's why I wrote this article. If you want to know only how to write, this item is recommended through.

Currently learning the Java language, programming itself is a beginner. First, make something that works! In the spirit of that, all variables are described in public, and object orientation is postponed. This time, I decided to code with object-oriented programming, but I was addicted to errors in the setter and getter parts, and it took me a week to resolve them.

Even if I look it up with setters and getters, all that comes up is the stories of those who do not need it, and there is nothing that explains my error in grammar or functions, so I was very worried, so I wrote an article like this I am. The details of the error will be described later.

Prerequisite environment

Language Java-12.0.1 Written in a text editor

grammar

First of all, how to write setters and getters.

SetGet.java


//Variable declaration class
class SetGet{

  //Variable declaration in private
private type variable name;

  // getter
public type variable name{
    return this.Variable name;
  }

  // setter
public void variable name(Type Appropriate variable name){
    this.Variable name=Appropriate variable name;
  }

}

Next, how to write in the execution class

SetgetRunner.java


//Execution class
class SetGetRunner{
  public static void main(String[] args){
    //Creating class reference variables and instances
    //In this case, class name = SetGet
Class name reference variable=new class name();

    //Assign a value to a variable declared private in the SetGet class
Reference variable.set variable name(Value you want to assign);

    //Get value
Type variable name 2=Reference variable.get variable name();
  }
}

If you explain in Japanese, it will look like the above. However, it is very difficult to understand, so let's actually write it.

Setget2.java



//Classes for getters and setters
class SetGet2{
  //Declare int variable apple in private
  private int apple;

  // getter
  public int getApple(){
    return this.apple;
  }

  // setter
  public void setApple(int apple){
    this.apple = apple;
  }
}

//Execution class
class SetGet2Runner{
  public static void main(String[] args){
    //Creating reference variables and instances for classes
    SetGet2 setget2 = new SetGet2();
    
    //Assign to a variable declared as private
    setget2.setApple(3);
    
    //Get value
    int appleQuantity = setget2.getApple();
   
    //output
    System.out.println("Number of apples:" + appleQuantiry + "Ko");

    //Output result
    //Number of apples: 3
  }
}

It will be.

function

Next, I will explain the function. getters and setters "In object-oriented programming, variables in an instance represent an internal state and should not be directly referenced or manipulated from the outside. Be sure to go through a method that corresponds to access to variables. This has the advantage of being able to properly check and process within the method and control access from the outside so that there is no failure. "

getter "Method for reading / acquiring / referencing the value of member variables (attributes, properties) inside an object from the outside"   settter "Method for writing / manipulating / setting the value of member variables (attributes, properties) inside the object from the outside"

(Quoted from IT Glossary e-words)

a. Simply put, why the idea of object-oriented programming is used. It means restricting access from the outside to prevent errors and mistakes.

getter is a method for getting a value from the outside, and setter is a method for manipulating it from the outside.

As you can see from the above explanation, it actually works without a setter if you do not operate it from the outside. You need setter only to operate from an external class, so you don't need it if you just use variables. What you are doing in the execution class is just calling the getter and setter methods in the declaration class.

Specific operation content

I will explain specifically how each works using the Setget2.java file mentioned above.

Setget2.java


  Setget2 setget2 = new SetGet2();

By declaring a reference type variable and creating an instance, you can handle the Setget2 class. For reference type variables and instances, if you want to understand in Java, this HP is very easy to understand, so please refer to it.

Setget2.java


setGet2.setApple(3);

I am calling the setter method of the SetGet2 class using the reference type variable that I can handle earlier.

SetGet2.java


public void setApple(int apple){
  this.apple = apple;
}

It was written like this in the setter method. By argumenting the number 3 to int apple, the value argument to the variable apple of the SetGet2 class is assigned. That is what we are doing by assigning the value of the execution class.

Next is the movement on the value acquisition side.

Setget2.java


int appleQuantity = setget2.getApple();

In the execution class, the getter method of the SetGet2 class is called and assigned to the variable appleQuantity.

SetGet2.java


public int getApple(){
  return this.apple;
}

There is a return value here. You used a method to return apple, which you assigned the value in the setter earlier, to the execution class. AppleQuantity is declared as a box that handles the return value, and it is finally possible to handle variables of another class.

By the way, a new variable is declared again in the execution class and a variable of another class is used, but by making it a condition of the expression, it is possible to use a variable of another class without using a variable.

For example in another class

Mass.java


private int colum;
private int row;

Suppose you are declaring. After assigning the value with setter

MassRunner.java


ArrayList<Integer> data;

for(int i = 0; i < mass.getColum() * mass.getRow; i++){
  this.data = new ArrayList<Integer>;
  this.data.add(i);
}

By doing so, each value can be used without putting it in a variable. After all, you need a variable to assign the result of the expression ...

The error that occurred this time

So far, I finally arrived at the error content that I was addicted to for a week. The code where the error occurred is as follows.

Game.java


class Card{
  //Variable declaration
  private int mark;
  private int number;

  // getter setter
  public int getMark(){
    return this.mark;
  }

  public void setMark(int mark){
    this. mark = mark;
  }

  public int getNumber(){
    return this.number;
  }

  public void setNumber(int number){
    this.number = number;
  }
}

class Deck{
  Card card = new Card();
  card.setMark(4);
  card.setNumber(13);

  //Declaration of an empty deck
  ArrayList<Integer> deckCard = new ArrayList<Integer>();

  //Creating a deck
  public Create(){
    for(int i = 1; i < card.getMark() * card.getNumber(); i++){
    this.deckCard.add(i);
    }
  }
}

class Game{
  public static void main(String[] args){
    Deck deck = new Deck();
    deck.Create();
  }  
}


Error statement

error:Illegal type start
  card.setMark(4);
  ^
error: <identifier>there is not
  card.setMark(1);
               ^
error:Illegal type start
  card.setNumber(13);
  ^
error: <identifier>there is not
  card.setNumber(13);
               ^

Cause

As you can see by comparing it with the example sentence, set There is no main method in the class. I was able to get the value in a class without the main method, but it seems that I can't manipulate the value without the main function.

I didn't know that I could only set the value later in the main function, and I was addicted to it for a week because I only looked at the grammar of setters and getters. If you compare your own code with the reference code, it was an error because the range you saw was narrow even though it was one shot.

Please be careful, too. I don't understand why it can be executed only by the main method, so I'll wait for someone else to explain it.

I would like to ask knowledgeable people as to whether or not it is necessary in the first place.

Thank you for reading.

reference

August 11, 2014 First Edition by Kiyotaka Nakayama / Daigo Kunimoto, published by Impress Co., Ltd. [Introduction to Java 2nd Edition](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82] % 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9% E3% 83 % 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8% AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X) ISBN 9784-8443-3638-9

Accessed on July 4, 2019 Easy introduction to Java

Accessed July 4, 2019 Introduction to Java Object Oriented Programming

Accessed on July 4, 2019 IT Glossary e-word

Recommended Posts

java setter getter error resolution
Today's java error
About getter setter
ITMS-90626 Error Resolution
java error countermeasures
Java Math.sqrt Error Pit
ActionController :: InvalidAuthenticityToken error resolution
rails error resolution summary
Error resolution on Heroku
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
Confront Java Floating Point Error
Error when playing with java
Lombok's @Getter @Setter operation memo
Summary of java error processing
Automatic generation of constructor, getter / setter
Spring Boot + PostgreSQL error resolution method
Commit failed exit code 1 error resolution