-2

I try to learn JAVA language. I write simple program, but it don't work. Could somebody give to me advice how solve problem. My program:

class WriteOut {
    private static int sumas;
    public void sud(int sds) {  
        sumas = sds;
    }

    public static void main(String arg[]) {
        WriteOut sum =new WriteOut();
        sum.sud(5);
        System.out.println("suma: "+sum);
    }
}

Output I get "suma: bandymas.WriteOut@70dea4e";

I want get answer "suma: 5"

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • Override toString: Add a method to WriteOut: `public String toString(){return String.valueOf(this.sumas);}` – ernest_k Apr 21 '18 at 21:46
  • 1
    Possible duplicate of [How to use the toString method in Java?](https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Turing85 Apr 21 '18 at 21:48
  • Just a few hints for you. You need to look for encapsulation and think about what you are printing. Sum is an object and it seems that you are expecting to display the value of a field of the WriteOut class. Also, I don't think you want to have sumas as a static field as it means every instance of WriteOut would share the same sumas reference. – eric.v Apr 21 '18 at 21:48

2 Answers2

1

System.out.println() calls toString() method, which comes from basic Object class. There are two ways of fixing this code:

1) Override the standart toString() method:

class WriteOut {
    private static int sumas;
    @Override
    public String toString(){
        return String.valueOf(sumas); // returns a string with sumas value
    }

    public static void main(String arg[]) {
        WriteOut sum =new WriteOut();
        sum.sud(5);
        System.out.println("suma: "+sum);
    }
}

2) Or just change the System.out.println() call:

class WriteOut {
    public static int sumas; // To allow System.out.println() to see this variable
    public void sud(int sds) {  
        sumas = sds;
    }

    public static void main(String arg[]) {
        WriteOut sum =new WriteOut();
        sum.sud(5);
        System.out.println("suma: "+sum.sumas);
    }
}
Lev Leontev
  • 2,538
  • 2
  • 19
  • 31
  • Good answer, but, one proposal: If you add one third way using private+getter (I know, a bit too much boilerplate, but still a very common way in Java), you get my upvote! – acdcjunior Apr 21 '18 at 22:02
  • @vandench Nothing wrong at all. I myself question a lot the unnecessary boilerplate of the "JavaBean" convention. But having getters is still a reality (and probably will always be), so, to someone that is learning the language, it is an important convention to get acquainted to. – acdcjunior Apr 21 '18 at 22:17
0

change what's inside System.out.println() to :

System.out.println("suma: "+sum.sumas);

Exmple:

class WriteOut {
        private static int sumas;
        public void sud(int sds) {  
            sumas = sds;
        }

        public static void main(String arg[]) {
            WriteOut sum =new WriteOut ();
            sum.sud(5);
            System.out.println("suma: "+sum.sumas);
        }
    }

Output :

suma: 5
Abdo Bmz
  • 632
  • 1
  • 11
  • 24