0

I've currently been reading the documentation 3 times now, and I am having a hard time applying what is written to my own simple program. I'm trying to understand on a fundamental level why exactly I am writing p.add(v); in my code below.

I declare an object p, and an object v with the values of 5 and 17 respectively. My method add then adds the two values together. I takes on the argument v so I understand that the object v has to be in the brackets of add(v) when I refer to it in my main method. However I'm having a hard time explaining in words why I need to call on the method add with respect to p here. Isn't p also an argument like v? Why can't I just change my method to be:

public void add(Positive v, Positive p)

And then call it by saying add(v,p);?

According to the java documentation, I need to call on a method when the object is outside of my class? But everything here is inside my class? So I'm not sure what it is exactly that they're trying to say in the documentation.

public class Positive {
    public static void main(String[] args) {
            Positive p = new Positive(5);
            Positive v = new Positive(17);
            p.add(v);
            System.out.println(p.get());
    }
    private int n;

    public Positive(int n) {
        this.n = n;
    }

    public void add(Positive v)
    {
        this.n += v.get();
    }

    public int get() {
        return n;
    }
}
WoeIs
  • 1,083
  • 1
  • 15
  • 25

6 Answers6

1

So, You got a class named Positive, there you got a public static void main function. As you remember, Java programs start executing at the main method (read a bit this wiki: https://en.wikipedia.org/wiki/Entry_point#Java).

so, when you run this method you will execute the following:

Positive p = new Positive(5); Positive v = new Positive(17);

here you initiate 2 class instances, p and v by using the new operator (which, calls the constructor method).

p and v are two different instances of Positive.

each p and v has the methods listed in this class (here its add and get) each of them is unique to the each instance.

then you get the field n from v using the get method of v and add it to the n member of p (read more about instance methods vs static methods here: read more here about: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/) also read about java access levels for members (https://www.programcreek.com/2011/11/java-access-level-public-protected-private/)

now you wanna execute the add method of p by doing [first you will execute the get method of v]:

p.add(v.get());

because both of them are non static methods, they belongs to an instance, therefore you need this syntax.

;;;

regarding add(p,v); if add was declared as static and the arguments were Positive p, Positive v then you could do something like you suggested:

public class Positive {
public static void main(String[] args) {
    Positive p = new Positive(5);
    Positive v = new Positive(17);
    int result = add(p,v);
    System.out.println(result);
}
private int n;

public Positive(int n) {
    this.n = n;
}

public static int add(Positive p, Positive v)
{
    return p.get() + v.get();
}

public int get() {
    return n;
}
}

read more about static/instance methods.

user2212726
  • 1,225
  • 3
  • 16
  • 23
0

This call adds v's value into p.

if you just add v and p together where do you want to store the result?

What this call does, is to increment the private value n of your instance p by the value of v.

Simple said:

Start with: p = 5 v = 17

p.add(v) means: p = 22 (5 of p + 17 of v added) and stored in p. So p is now 22. call p.add(v) again and it will again increase p's value by 17, making it 39 total.

v is still 17 all the time.

just for fun you could call: p.add(v) p.add(v) v.add(p)

the first two adds bring p to 39 as I said above, if you now add p into v, you add those 39 to the 17 of v and store the result in v (because you invoke v.add). v is 56 then.

I hope this makes it a bit clearer what the method calls to. Cheers Gris

Grisgram
  • 3,105
  • 3
  • 25
  • 42
0

You can also have a static method that covers public void add(Positive v, Positive p) like this:

public static void add(Positive v, Positive p)
{
    p.n += v.get();
}

Please also read my answer here that will explain more about calling from objects and static calls.

Mohsen
  • 4,536
  • 2
  • 27
  • 49
0

There's a subtle difference between p as an argument to add(v,p) and p as the object on which the method add(v) is invoked.

When you look at p as an object, a Positive, then it becomes distinct from the number that it holds. In common terms, we say that p is a Positive object holding a number n. The number (n) in the Positive object can change while p, the object itself remains the same.

In other words, add(v,p) would be adding two Positives (and would be typically defined as a static method), whereas p.add(v) would be modifying the number on the positive p (typically declared as an instance method)..

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

There is a difference between static and non-static methods.

Static methods are basically regular methods with some additional access permissions.

Non-static methods on the other hand receive an additional hidden "this" parameter, with which you convey the context of the call. That way you can implement OOP features like encapsulation, inheritance and polymorphism.

For example, the non-static method Positive.add(Positive v) could be made virtual and be overridden in a subclass ComplexPositive. Then you can continue working with Positive objects, but the calls to add() will depend on each instance of the object. You can't achieve that easily with static methods.

rustyx
  • 80,671
  • 25
  • 200
  • 267
0

I think you should start from the class.

A class is a set of data and methods.

The methods are a kind of message you can sent to an object of the type of a specific class.

Those messages can be a query, that returns to you a some of the data contained in that object or just the data in a different format.

Or you can send a command to an object, in order to change the data contained in the object.

What is the object?

It is just an instance of your class, that means it is a part of memory that contains specific data organized in the way you have defined your class.

In you code example, you're defining a class Positive that represent an integer that can be added.

So you write a command add(Positive p) and a query get().

The main() method in Java is a special method that is used from the JVM at runtime to execute your program.

It is an entrypoint.

As Java is based on classes everything should be placed inside a class, so even the main() method.

Why you have to call a method in order to add 2 Positive?

The reason is because you want to hide the details about this operation.

In your case is trivial as it is only a sum of integers, but in a normal case can be very complex and you don't want the logic to be spread all around your code base.

So you put the logic to query and change the data inside classes to avoid complexity and issues when you add new feature or just fix a bug.

In your scenario there is no reason to write a class with methods, you can just do it inside the main method with the same result:

public class Positive {
    public static void main(String[] args) {
        int a = 5;
        int b = 17;
        System.out.println(a+b);
    }
}

Or Just:

public class Positive {
    public static void main(String[] args) {
        System.out.println(5+17);
    }
}
Mario Santini
  • 2,905
  • 2
  • 20
  • 27