Creating new Class Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Let say i got a class name resident.
When a new resident want to register, it will create a new object in the resident class and all related information is stored.
And the resident detail need to write into a file and store.
Can someone briefly tell me what to do and what method should i use? Thanks.

You'll need instance variables for the things you want to track, for example firstName, lastName, address. You can set and retrieve these values by making getters and setters for every instance variable. You could also make a constructor which assings the given arguments to the instance variables when you create a new resident, since every resident would probably need at least a firstName and other data.

Related

What is the best practice to store state variables in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm new to Java.
Say I want to make a simple game with Swing or whatever. How and where do I store variables like the player's score or progress, for example, so that I can access it from different classes (during the game and before persistence in the database). I like how we use useContext in React. I also used global variables in PHP's sessions.
I highly advice to not use a "global state". I would rather recommend to implement a domain object or service that fetches high scores. This service can then be injected and called where needed.
Using a domain object or a service to access and modify domain data has the benefit that ownership is clearly defined: the business object/service owns the data, and the data is only accessed and modified through the business object/service.

What is the benefit of writing an object to a file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I understand we can do this with file input and output, but why would we want to do this?
It is simply called persistence.
You nailed it: you want to be able to store information (for example after intensive computations) in a way that survives the lifetime of the current JVM process.
In that sense serialization is a (poor) version of database storage.
But of course, that comment is correct: this does not prevent the creation of objects. It is a mechanism to resurrect previous state into "new" objects.

program name unnecessary to be same as class name- c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I read in an online article that in java, the program file name should be same as class name. Why is that?
Also in c# i read that program file name need not be same as class name. Why?
Can someone please explain the difference?
Here is a link to the article:
https://www.tutorialspoint.com/csharp/csharp_program_structure.htm
A Java class file can contain multiple classes and you can name them whatever you want.
But ideally, following the best practices, it is better to write one class per file and name the file as per the name of the Class.

Best way to maintain a list in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a list of objects which get created by a factory class and for the time being I want to limit the list size to 10 objects. I also need to maintain the list through out the program as multiple classes will be referencing its values. I don't want to create it as a singleton and im not happy about having a master class maintain it, so is there another way? (Best Practice)
For setting the max size you can refer to this post here Any way to set max size of a collection?
Not sure what you mean by "maintaining" the list though. There is no way (that I know of) to make that list accessible to all classes without having it in a "master class". The only other option is passing it through functions.

What is the best type to store multidimensional data type in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
what is the best type to store data from csv format file, where is 150k records(lines) where each line has 9 columns(fields). After initialize my type with these data i need to do some operations on it, like add record in specified line, or add/change some data in some field. Also i need to compare data between themselves. Thank for help!
The best type to use is an ArrayList of a domain object. The domain object has 9 properties.
In other words: don't make it generic, make it specific for your problem. Then your code will be more readable and type-safety helps you when you create the functionality.

Categories