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 3 years ago.
Improve this question
I am relatively new to java and don't understand why the setter method is a static context when I haven't used "static" anywhere.
public class Appointment {
LocalDateTime Time;
Doctor Doctor;
Patient Patient;
String Notes;
public Appointment(LocalDateTime time, Doctor doc, Patient pat, String notes){
Time = time;
Doctor = doc;
Patient = pat;
Notes = notes;
}
public void setNotes(String Notes) {
Appointment.Notes = Notes;
}
}
EDIT: Someone commented the answer so I cant mark it as correct but I put Appointment.Notes instead of this.Notes
This is the syntax for assigning a static variable (not necessarily a member of the current class).
public class Appointment {
Appointment.Notes = Notes;
Instead write:
this.Notes = Notes;
Or better change the variable name to something standard:
this.notes = notes;
I strongly suggest sticking to naming conventions. Also it is useful to quote the actual compiler error message in questions.
Related
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 2 years ago.
Improve this question
public class Student {
public Student(String name){
do_smth(name);
}
public Student(){
this("Mike");
}
}
How force developers to use parameterized constructor only if value differs from default:
not calling new Student("Mike") but use for this new Student()?
The reason: we have constructor with 5 parameters. In most cases parameters are the same. But there are about 5%-10% cases when they differ.
 So in order to avoid duplications, I would like to use such approach.
I know it maybe better to use smth like Builder pattern here. But I don't like it verbosity.
This may be implemented by using additional private constructor with a flag:
public class Student {
public Student(String name) {
this(name, false);
}
public Student() {
this("Mike", true);
}
private Student(String name, boolean defaultUsed) {
if (!defaultUsed && "Mike".equals(name)) {
throw new IllegalArgumentException(
"Tut-tut lil kid, it's pwohibited to set Mike's name outside defauwt constwuctor");
}
do_smth(name); // only if do_smth cannot be overridden in child classes
}
}
Note: method do_smth should be private or final so that it could not be overloaded in subclasses which is far more important than setting a limit on setting a name from specific constructor.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
We are creating an enum for possible environments the user can login to. But in my team, we have a disagreement on what is the better way to proceed. I'm hoping to get some insight from the community, so that we can all learn.
The aim is to create an enum with environments (prod, staging, etc..)
Each environment, has a baseURL, a loginURLPostfix and a logoutURLPostfix.
What is the better way to go about this? And please explain why.
Method 1
public enum Environment {
Development,
Production;
//
// Constants
//
private static String BASE_URL_STRING_PROD = "https://prodBaseURL.com";
private static String BASE_URL_STRING_DEV = "https://devBaseURL.com";
private static String LOGIN_SUFFIX = "/login";
private static String LOGOUT_SUFFIX = "/logout";
public String loginURLString() {
return baseURL() + LOGIN_SUFFIX;
}
public String baseURL() {
switch (this) {
case Development: return BASE_URL_STRING_PROD;
case Production: return BASE_URL_STRING_DEV;
default: return REDIRECT_URI_PROD;
}
}
}
Method 2
public enum Environment {
Development(Constants.BASE_URL_STRING_DEV, Constants.LOGIN_SUFFIX),
Production(Constants.BASE_URL_STRING_, Constants.LOGIN_SUFFIX);
String baseURL;
String loginURLSuffix;
public Environment(String baseURL, String loginURLSuffix) {
this.baseURL = baseURL;
this.loginURL = loginURL
}
public String loginURLString() {
return this.baseURL + Constants.LOGIN_SUFFIX;
}
}
public class Constants {
public static final String BASE_URL_STRING_PROD = "https://prodBaseURL.com";
public static final String BASE_URL_STRING_DEV = "https://devBaseURL.com";
public static final String LOGIN_SUFFIX = "/login";
public static final String LOGOUT_SUFFIX = "/logout";
}
Edit: StackOverflow put the question on hold as an "opinion based" question. So my edit in order to clarify this is the following:
Is the difference between Method 1 and Method 2 purely opinion based?
I don't like either approach for the properties you are trying to set.
What you have here is environment configuration in code, which is something that you should avoid as it requires a code change should any of these parameters change. Environment URL's would be best suited to a configuration file (XML, YAML, .properties, pick your poison) rather than hard-coded into your source code.
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 want to create a basic Item class hierarchy.
Each Item will have a String name and an int itemID.
These variables will be set in each subclass of Item.
The Item class declares the two variables and each subclass of Item
(public class Hammer extends Item) will set the name and itemID.
For example, name = "hammer" and itemID = 01.
How would I do this efficiently and effectively?
The base class will declare two final private fields (name and itemid).
The values for these will be set via the base class constructor.
Each of the derived classes will call the base class constructor with the appropriate name and itemid value.
for example:
public abstract class Blam
{
private final int itemId;
private final String name;
protected(
final int itemId,
final String name)
{
this.itemId = itemId;
this.name = name;
}
}
public class Derived
extends
Blam
{
public Derived()
{
super(123, "nameValue");
}
}
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 want to implement this singleton class in java . How can I
put these variables in this class?
This is one simple example for your singleton. Feel free to add setters and getters as you need it for the fields.
I'm not sure what the set-method in your diagram should do but maybe you don't need it anyway.
public class LibraryInfo {
private static final LibraryInfo instance = new LibraryInfo();
public static LibraryInfo getInstance() {
return instance;
}
private LibraryInfo() {}
private String name;
private int phone;
private String address;
private String openTime;
private String closeTime;
// getters
}
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 9 years ago.
Improve this question
I want to know the exact scenario of using constructor over methods can anyone give me the exact example program for constructors over methods in java
They are not similar things to compare even.
Both serves completely different purposes and even you have to note that constructor wont return anything, not even void :)
If you see a basic tutorial on Constructor, mentioned
Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
So you cannot choose one over them.
If you are looking/talking about setting variables of instance memebers, choose setter methods instead of variables.
Another scenoriao is some objects never complete without providing some basic info. In that cases you have to create a constructor like it should be built when necessary info passed in constructor.
Consider the below scenorio, where to create an employee class, He must have an employee Id
public class Employee {
String empId;
public Employee(String empId) {
this.empId = empId;
}
// Methods
public static void main(String[] args) {
Employee a = new Employee("green");
}
Consider the below scenorio, where to create an empty employee class, later he can assign employee Id
public class Employee {
private String empId;
public Employee() {
}
// Methods
public void setEmpId(String empId) {
this.empId = empId;
}
public static void main(String[] args) {
Employee a = new Employee(); //No error
a.setEmpId("SOMEX007");
}
}