Creating an object of an entire class?
I'm trying to write a program that can save an inpatient or an outpatient, I have my service classes and im trying to write my application class, and I'm trying to create an object of one of the service classes, but it requires i put in the arguments of the sections inside the service class of the same name. any advice on what do to? im trying to make an object of the whole class, so i can then save user inputs to the variables inside the class?
sorry if my question doesnt make sense, im learning java and clearly its not going too well😅
10 Replies
⌛
This post has been reserved for your question.
Hey @AyoWut?! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Hey! Are you familiar with Spring? The pattern I see most often is to use @AllArgsConstructor which will generate a constructor that takes in the necessary objects (beans in spring)
Since he is at the start of java i would say not also @AllArgsConstructor is a part of lombok
https://projectlombok.org/features/
this is how you can create a obj
https://www.w3schools.com/java/java_classes.asp
https://www.tutorialspoint.com/java/java_object_classes.htm
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Java - Classes and Objects
Java - Classes and Objects - Java is an Object-Oriented programming language. In Java, the classes and objects are the basic and important features of object-oriented programming system, Java supports the following fundamental OOPs concepts –
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
That’s fair. I’d say it’s also important to remember every object is an object of an entire class! Services ‘process’ other objects but both service classes and model classes are objects at runtime (unless static)
i guess what im trying to say is that it requires i put the arguments that match little sections within the class (that are labeled as the same name) whereas im trying to get it to run through the whole class rather than just one section, maybe im misunderstanding the concept?
thank you! ill look at these!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
When you say run through the whole class what do you mean?
An object has member variables (these exist in the context of the whole object)
and methods. Methods are how other objects can interact with your object, if there is logic that you want the service to do it will have to be executed through a method.
Methods take parameters which look like
yourObjectName.methodName(parameter1, parameter2). Lets say you have a class that does a lot of different things and you have one method that does something simple like add. when you want to run that method you only need to call the method with the necessary inputs like yourObjectName.methodName(number1, number2) and that will be fine. BUT the yourObjectName object has to be created at some point (unless it is static). If your method is not static, Java assumes that the method related to the object itself. It cannot know what you may or may not need from the object as a whole, in fact it assumes you will need that stuff if it is not marked as static.
So if you have a Service class that does something, you will need to make sure the service is created using an appropriate constructor (the thing that makes the object at runtime) That is probably why it is asking for things that aren't directly related. Does taht help at all?
The flow is like this
create object -> use object methods -> methods are pieces of code that DO something
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.