The basics of several classes by placing a print class in the main method

I am trying to see the basics of what is required to be called in the second grade, because the textbooks and the book I use overly complicate it using user input right now.

So here is what I tried. First, my main class, and the second class, which I tried to call in the main method, depicting plain text.

public class deck {
    public static void main(String[] args) {
    edward test = new edward();
    System.out.print(test);
    }
}

Another class:

public class edward {
    public void message(int number) {
        System.out.print("hello, this is text!");   
    }
}

Why is this not working?

If you could try to explain what I am doing or how it works in a little detail, that would be nice. I find it difficult to discourage this part.

+4
source share
3 answers

, : test , :

public class deck {
    public static void main(String[] args){
        edward test = new edward();
        test.message(123);
    }
}

message(int) - ( , ). , , ( , test), .

static - , main. , .

+5

.

Java java, "Object".

.

public String toString()

.

edward .

public class edward {

    @override
   public String toString() {
        return "hello, this is text!"

   }

}

edward (test) t , ,

    public static void main(String[] args) {
          edward test = new edward();
          System.out.println(test);
    }

returnrd overriden toString(). (Object) (edward).

, toString () , .

toString, , , ​​ @ae23da, .

+4
public class deck
{
    public static void main(String[] args)
    {
        edward test = new edward(); //1
        System.out.print(test); //2
    }
}

1 edward test.
2 print . API Java, print(Object)

. , String.valueOf(Object), , , write(int).

, : edward@672563. , String.valueOf(obj) obj (edward), @, obj (672563).


, , :

public class Deck //all class names should be capitalized
{
    public static void main(String[] args)
    {
        Edward test = new Edward();
        test.message(); //1
    }
}

public class Edward
{
    public void message() //`message` doesn't need a parameter
    {
        System.out.print("hello, this is text!");
    }
}

On the line, 1you call the method test message(). The method call executes the code that is in this method, therefore it test.message()executes the line

System.out.print("hello, this is text!");

Here is another way to do the same:

public class Deck
{
    public static void main(String[] args)
    {
        Edward test = new Edward();
        System.out.println(test.message); //1
    }
}

public class Edward
{
    public String message = "hello, this is text!"; //2
}

In the line, 2you create a new field Stringwith a value "hello, this is text!". In the line, 1you print the value of the field messagecontained in the object test.

If there are other parts of this code that you don’t understand, feel free to comment on this answer!

+3
source

All Articles