Reputation: 45
I want to print the default private bloodtype
and rhfactor
which is O+
and +
I want to print it from another class which has the main method.
I've already tried creating new objects and printed them but still it says that I'm accessing a private variable. When you input something on the scanner
it prints it but if you input none I want to print the constructor with private variables!
public class blooddata {
private String bloodtype;
private String rhFactor;
blooddata(){
bloodtype = "O";
rhFactor = "+";
}
blooddata(String btx, String rhx){
this.bloodtype = btx;
this.rhFactor = rhx;
}
public String getblood (String bloodtype){
return bloodtype;
}
public String getfactor (String rhFactor){
return rhFactor;
}
public void setblood(String bloodtype){
this.bloodtype = bloodtype;
}
public void setfactor(String factor){
this.rhFactor = factor;
}
}
here is the class that has main method
import java.util.Scanner;
public class Runblooddata {
static Scanner sc = new Scanner(System.in);
static String btx;
static String rhx;
public static void main(String[] args) {
System.out.print("Enter blood type: ");
btx = sc.nextLine();
System.out.print("Enter rhFactor: ");
rhx = sc.nextLine();
if (btx.isEmpty() || rhx.isEmpty()){
blooddata asd = new blooddata(); //this is where i am lost
}else{
blooddata bd = new blooddata();
bd.setblood(btx);
bd.setfactor(rhx);
System.out.println(bd.getblood(btx));
System.out.println(bd.getfactor(rhx));
}
}
}
Upvotes: 1
Views: 876
Reputation: 4460
As Andrew has already explain the design issue what you have in your code, I'll guide you towards the solution what you are seeking for.
blooddata bd = new blooddata();
if (!btx.isEmpty() && !rhx.isEmpty()){
bd.setblood(btx);
bd.setfactor(rhx);
}
System.out.println(bd.getblood());
System.out.println(bd.getfactor());
Upvotes: 1
Reputation: 49606
Getters aren't supposed to have parameters.
When you declare a method parameter named the same as a field, the parameter hides the field. You basically return the parameter you take.
6.4.1. Shadowing
A declaration d of a field or formal parameter named n shadows, throughout the scope of d, the declarations of any other variables named n that are in scope at the point where d occurs.
public String getBlood() {
return bloodtype;
}
public String getFactor() {
return rhFactor;
}
I will help you to simplify it a bit.
final class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter blood type: ");
String btx = sc.nextLine();
System.out.print("Enter rhFactor: ");
String rhx = sc.nextLine();
BloodData data = btx.isEmpty() || rhx.isEmpty() ?
new BloodData() :
new BloodData(btx, rhx);
System.out.println(data.getBloodType());
System.out.println(data.getRhFactor());
}
}
final class BloodData {
private final String bloodType;
private final String rhFactor;
BloodData() {
this("O", "+");
}
public BloodData(String bloodType, String rhFactor) {
this.bloodType = bloodType;
this.rhFactor = rhFactor;
}
public String getBloodType() {
return bloodType;
}
public String getRhFactor() {
return rhFactor;
}
}
Upvotes: 4