r/javahelp • • Aug 23 '25

What are this three brothers?

This brothers are so confusing me a lot ,yes you heard it right,I have started learning java recently however I have been facing this confusion in between what is exactly the difference among attributes,methods and constructors.

Anyone kindly can explain this trio's diff...

Thank you in advance.

0 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Aug 24 '25

So ,when should i use the static method or public method is there any scenario?

2

u/ITCoder Aug 24 '25 edited Aug 24 '25

public methods can be accessed from any other class, given that class is public too and not default / package private (check encapsulation and access modifiers for better understanding)

Let's say you have a package for payments. It has a public class CreditCard and public method calculatePayment, and in the method logic is there to calculate payment amount after deducting gift card amount and discount percentage.

You can create object of this class in any other package like processPayments in your project (or in any other java project / application, given your project is added as a dependency, which is how real life project works), and call the public method calculatePayment there.

As an example lets say your Visa credit card has reward points, and they have coded the logic to apply some percentage of reward points towards your payment, based on what kind of customer you are, silver or gold level, in their application. You use your visa card to make payment at amazon website. In amazon code, they will have logic to call the public method of the public class that Visa had provided them, and then using this method, they calculate final amount.

Any method that needs to be accessed from another class ( be in the same or another java project) should be public. In common lingo, another java project using (consuming) it is called client.

Static methods and variables (attributes), are shared by all objects of that class (instances). In previous reply I mentioned, each object has its own copy of non static attributes and methods, which is independent of each other. Static is opposite of it.

You have an application form to fill, all fields of the form would have a default value of say 0 or null or empty, which you will populate. But for easy maintenance I need to assign an application id to your form. A user should not be able to fill any random value there.

I will create a static attribute applicationId = 0 initially. And i will have a static method, where I will put the logic that every time a form (object) is submitted, increment the application id by 1 and update the variable.

A submitted first form, application id is 1 now. B submitted second form, here the second object see that the value of application id is 1, because static variables and methods are shared by all objects of a given class ( hence they are also called class variables and class methods and non static variables are called instance variables and methods), and call the static method, which increment this value to 2. Just so you know, an instance of a class means an object of the class, and instantiate a class means, creating an object of the class.

Static variables and methods are used for utility or helper methods, for the scenario where the logic or value needs to be shared by all objects, such as getDate(), geTime(), getId()

Note that, static methods cannot use non static variables, they deal with static variables only, as they operate on class level and not object level. (It's also related to class loading, but thats too advanced a topic and hardly needed here). Also due to this, you can and should call static method as ClassName.staticMethodName(). You can call static methods on object also, but that is a BAD practice.

1

u/[deleted] Aug 24 '25

tqsm for detailed exp

2

u/ITCoder Aug 24 '25

Just checked those W3schools links in another comment, and man what a bad way to learn, esp if you are a newbie, giving 2/3 lines of explanation, if i can call it that.

I would suggest Head First Java book to learn these concepts. It was much easier and fun way to learn these concepts, esp with their do it yourself. I never got bored reading those when I started learning it. dm me for pdf, if u want.