I'm new into Java and want to improve my OOP skills.
Therefore I try to write all my "first programmes" object-oriented.
Anyway... I started a small primitives test programme:
public class Primitives {
byte b;
private void setByte (byte b)
{
this.b = b;
}
public byte getByte()
{
return b;
}
short s;
private void setShort (short s)
{
this.s = s;
}
public short getShort()
{
return s;
}
int i;
private void setInteger (int i)
{
this.i = i;
}
public int getInteger()
{
return i;
}
long l;
private void setLong (long l)
{
this.l = l;
}
public long getLong()
{
return l;
}
float f;
private void setFloat (float f)
{
this.f = f;
}
public float getfloat()
{
return f;
}
double d;
private void setDouble (double d)
{
this.d = d;
}
public double getDouble()
{
return d;
}
boolean bool;
private void setBoolean (boolean bool)
{
this.bool = bool;
}
public boolean getBoolean()
{
return bool;
}
char c;
private void setChar (char c)
{
this.c = c;
}
public char getChar()
{
return c;
}
String str;
private void setString (String str)
{
this.str = str;
}
public String getString()
{
return str;
}
public static void main(String[] args) {
Primitives prim = new Primitives();
prim.setBoolean(true);
//prim.setByte(42);
//prim.setChar("ft");
prim.setDouble(42.42);
//prim.setFloat(42);
prim.setInteger(42);
prim.setLong(424242);
//prim.setShort(0);
prim.setString("fourtytwo");
//System.out.println(integer.getInteger());
}
}
Afterward, I'm trying to call my getters in another class "Main":
public class Main {
public static void main(String[] args)
{
Primitives object = new Primitives();
int objectInt = object.getInteger();
String objectString = object.getString();
System.out.println(objectInt);
System.out.println(objectString);
}
}
My output is
0
null
How does this come?
I mean, I instanced (getInt for example) before, why I received 0?
I know there must 1000 answers here but I couldn't find any proper one.
Another question:
Why do I get "The method setByte(byte) in the type Primitives is not applicable for the arguments (int)" in my Setter? (that's why I comment some of the types out)
You should call the setter methods and set the value before you get them.
The problem with your code is : You are having two main classes and looks like you are running the one in the Main class which doesn`t initiate your variable.
The other main method inside your Primitives class never get executed, so you have not initiated your variables actually!
Each application should have one main class, and only one
Could you point me to the JLS section where it is specified that inherited methods will not act on subclasses re-defined variables?
I.e. the output of the following code is "value is 3" and not "value is 5".
public class PlayGround {
int value = 3;
public int getValue() {
return value;
}
public static void main(String[] args) {
PlayGround.PlayGroundSon pg = new PlayGround().new PlayGroundSon();
System.out.println("value is "+pg.getValue());
}
class PlayGroundSon extends PlayGround{
int value = 5;
}
}
You have not "re-defined" value. You have created a completely separate field in PlayGroundSon that happens to have the same name.
You can only override methods. If you want the program to print 5 you will have to override the getValue() method. I have also changed the name of the variable in PlayGroundSon to emphasize that it is not the same as value in PlayGround.
public class PlayGround {
int value = 3;
public int getValue() {
return value;
}
public static void main(String[] args) {
PlayGround.PlayGroundSon pg = new PlayGround().new PlayGroundSon();
System.out.println("value is "+pg.getValue());
}
class PlayGroundSon extends PlayGround{
int sonValue = 5;
#Override
public int getValue() {
return sonValue;
}
}
}
I want each element of an enum to have different variables but I can't reach them.
public class Employee {
public GENERAL[] general = GENERAL.values();
public static void main(String[] args) {
Employee e = new Employee();
e.general[GENERAL.INCOME.ordinal()].salary = 10; //this line doesn't compile
}
enum GENERAL{
INCOME{
public int salary;
public int tips;
},SATIFACTION{
//some variables
},EFFICIENCY{
//some variables
};
}
}
I've tried casting to (GENERAL.INCOME) but it didn't work. Is there a way to do it? If this is not possible, what is the best work around? Thanks in advance.
Try defining variables at enum level rather than individual elements:
public static void main(String[] args) {
MainClass e = new MainClass();
e.general[GENERAL.INCOME.ordinal()].salary = 10; //this line doesn't compile
System.out.println(e.general[GENERAL.INCOME.ordinal()].salary);
}
enum GENERAL{
INCOME(0,0), SATIFACTION(0, 0), EFFICIENCY(0,0);
int salary;
int tips;
GENERAL(int salary, int tips){
this.salary = salary;
this.tips = tips;
}
}
This is because INCOME is an anonymous subclass of GENERAL, it is something like this
static class GENERAL {
public static GENERAL INCOME = new GENERAL() {
public int salary;
public int tips;
};
}
there is no way to access fields of an anonymous class in Java (except reflection)
This is the cleanest way I can do it. I still have an array that I can use to iterate. Each element of the General holds its own variables. Each element has an ordinal to use as the index number.
The problem with this approach is this cannot make use of GENERAL.values(). If a new element is added later, It must be added to the getList() method manually and in the correct order. It is easy to make mistakes when adding new elements to the code.
public class Employee {
public Object general[] = General.getList();
public static void main(String[] args) {
Employee e = new Employee();
General.Income i = (General.Income) e.general[General.Income.ordinal];
i.salary = 10; //eclipse doesn't let me to combine these 2 lines into 1 expressions.
System.out.println(i.salary);
// following lines demonstrates that the salary of the e.general[General.Income.ordinal] is changed. Not just the i.
General.Income t = (General.Income) e.general[General.Income.ordinal];
System.out.println(t.salary);
}
public static class General {
public static Object[] getList() {
Object general[] = { new Income(), new Satisfaction(), new Efficiency() };
return general;
}
public static class Income {
public static final int ordinal = 0;
public int salary;
public int tips;
}
public static class Satisfaction {
public static final int ordinal() {return 1;}//using method instead of int saves memory. (8 bytes I think. Neglettable).
// some variables
}
public static class Efficiency {
public static final int ordinal = 2;
// some variables
}
}
}
If each enumeration would contain a single value, why not use that?
You can even add a method to retrieve some descriptive name:
enum General {
INCOME, SATIFACTION, EFFICIENCY;
int value = 0;
String getName() {
switch(this) {
case INCOME:
return "salary";
case SATIFACTION:
return "etc";
}
}
}
These can be set/get by General.values()[i].value and General.INCOME.value or add setValue(int value) and getValue() methods and make value private.
I have a problem with the inheritance of classes.
here is my Java code:
public class spaarRekening extends rekening{
private double rente;
public double getRente(){
return rente;
}
spaarRekening(String rN, int s, double d){
super(rN, s);
rente = d;
}
spaarRekening sR = new spaarRekening("456", 999999, 2.5);
}
public static class rekening implements rekeningIF{
int saldo;
static String rekeningNummer;
rekening(String rN, int s){
rekeningNummer = rN;
saldo = s;
}
public static String getRekeningNummer(){
return rekeningNummer;
}
public int getSaldo(){
return saldo;
}
}
I want to let the spaarRekening be printed out by:
System.out.printf("Uw rekeningnummer is: %s\n", spaarRekening.getRekeningNummer());
But it prints out null.
Why is this?
Thanks.
Don't use static. Not until you understand how it works and where it is applicable.
Since you never create any object of type spaarRekening or rekening the variable rekeningNummer never gets initialized, because you do intialization inside the constructor.
You most probably want to make your rekeningNummer non-static when you initialize it inside the constructor.
When you really want it to be static then you should initialize it in a static way, too. Therefore remove the code from the constructor and initialize it right away when declaring.
public class SpaarRekening extends Rekening{
private double rente;
SpaarRekening(String rN, int s, double d){
super(rN, s);
rente = d;
}
public double getRente(){
return rente;
}
public static void main(String[] args) {
SpaarRekening sR = new SpaarRekening("456", 999999, 2.5d);
System.out.println(sR.getRekeningNummer());
}
}
public class Rekening implements RekeningIF{
protected int saldo;
protected String rekeningNummer;
Rekening(String rN, int s){
rekeningNummer = rN;
saldo = s;
}
public String getRekeningNummer(){
return rekeningNummer;
}
public int getSaldo(){
return saldo;
}
}
Your rekeningNummer is a static variable and of typeObject and in your code. The variable has not been intialized, hence it printed null.
You have to initialize the static variable before using it:
public static class Rekening implements RekeningIF{
int saldo;
static String rekeningNummer = ""; // initialized to empty String
Rekening(String rN, int s){
rekeningNummer = rN;
saldo = s;
}
public static String getRekeningNummer(){
return rekeningNummer;
}
// Provide method for setting value of rekeningNumber
public static void setRekeningNumber(String number) {
rekening.rekeningNummer = number;
}
public int getSaldo(){
return saldo;
}
}
You never initialize rekeningNummer, and the default value is null.
You never initialized the rekeningNummer, you may think that the statement spaarRekening sR = new spaarRekening("456", 999999, 2.5);within yourspaarRekening class will initialize it, but you are putting it within class definition, you are not actually instantiating any spaarRekening instance. Remove that statement, and instantiating it at some other places in your code
I am trying to return 2 values from a Java method but I get these errors. Here is my code:
// Method code
public static int something(){
int number1 = 1;
int number2 = 2;
return number1, number2;
}
// Main method code
public static void main(String[] args) {
something();
System.out.println(number1 + number2);
}
Error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at assignment.Main.something(Main.java:86)
at assignment.Main.main(Main.java:53)
Java Result: 1
Instead of returning an array that contains the two values or using a generic Pair class, consider creating a class that represents the result that you want to return, and return an instance of that class. Give the class a meaningful name. The benefits of this approach over using an array are type safety and it will make your program much easier to understand.
Note: A generic Pair class, as proposed in some of the other answers here, also gives you type safety, but doesn't convey what the result represents.
Example (which doesn't use really meaningful names):
final class MyResult {
private final int first;
private final int second;
public MyResult(int first, int second) {
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
// ...
public static MyResult something() {
int number1 = 1;
int number2 = 2;
return new MyResult(number1, number2);
}
public static void main(String[] args) {
MyResult result = something();
System.out.println(result.getFirst() + result.getSecond());
}
Java does not support multi-value returns. Return an array of values.
// Function code
public static int[] something(){
int number1 = 1;
int number2 = 2;
return new int[] {number1, number2};
}
// Main class code
public static void main(String[] args) {
int result[] = something();
System.out.println(result[0] + result[1]);
}
You could implement a generic Pair if you are sure that you just need to return two values:
public class Pair<U, V> {
/**
* The first element of this <code>Pair</code>
*/
private U first;
/**
* The second element of this <code>Pair</code>
*/
private V second;
/**
* Constructs a new <code>Pair</code> with the given values.
*
* #param first the first element
* #param second the second element
*/
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
//getter for first and second
and then have the method return that Pair:
public Pair<Object, Object> getSomePair();
You can only return one value in Java, so the neatest way is like this:
return new Pair<Integer>(number1, number2);
Here's an updated version of your code:
public class Scratch
{
// Function code
public static Pair<Integer> something() {
int number1 = 1;
int number2 = 2;
return new Pair<Integer>(number1, number2);
}
// Main class code
public static void main(String[] args) {
Pair<Integer> pair = something();
System.out.println(pair.first() + pair.second());
}
}
class Pair<T> {
private final T m_first;
private final T m_second;
public Pair(T first, T second) {
m_first = first;
m_second = second;
}
public T first() {
return m_first;
}
public T second() {
return m_second;
}
}
Here is the really simple and short solution with SimpleEntry:
AbstractMap.Entry<String, Float> myTwoCents=new AbstractMap.SimpleEntry<>("maximum possible performance reached" , 99.9f);
String question=myTwoCents.getKey();
Float answer=myTwoCents.getValue();
Only uses Java built in functions and it comes with the type safty benefit.
Use a Pair/Tuple type object , you don't even need to create one if u depend on Apache commons-lang. Just use the Pair class.
you have to use collections to return more then one return values
in your case you write your code as
public static List something(){
List<Integer> list = new ArrayList<Integer>();
int number1 = 1;
int number2 = 2;
list.add(number1);
list.add(number2);
return list;
}
// Main class code
public static void main(String[] args) {
something();
List<Integer> numList = something();
}
public class Mulretun
{
public String name;;
public String location;
public String[] getExample()
{
String ar[] = new String[2];
ar[0]="siva";
ar[1]="dallas";
return ar; //returning two values at once
}
public static void main(String[] args)
{
Mulretun m=new Mulretun();
String ar[] =m.getExample();
int i;
for(i=0;i<ar.length;i++)
System.out.println("return values are: " + ar[i]);
}
}
o/p:
return values are: siva
return values are: dallas
I'm curious as to why nobody has come up with the more elegant callback solution. So instead of using a return type you use a handler passed into the method as an argument. The example below has the two contrasting approaches. I know which of the two is more elegant to me. :-)
public class DiceExample {
public interface Pair<T1, T2> {
T1 getLeft();
T2 getRight();
}
private Pair<Integer, Integer> rollDiceWithReturnType() {
double dice1 = (Math.random() * 6);
double dice2 = (Math.random() * 6);
return new Pair<Integer, Integer>() {
#Override
public Integer getLeft() {
return (int) Math.ceil(dice1);
}
#Override
public Integer getRight() {
return (int) Math.ceil(dice2);
}
};
}
#FunctionalInterface
public interface ResultHandler {
void handleDice(int ceil, int ceil2);
}
private void rollDiceWithResultHandler(ResultHandler resultHandler) {
double dice1 = (Math.random() * 6);
double dice2 = (Math.random() * 6);
resultHandler.handleDice((int) Math.ceil(dice1), (int) Math.ceil(dice2));
}
public static void main(String[] args) {
DiceExample object = new DiceExample();
Pair<Integer, Integer> result = object.rollDiceWithReturnType();
System.out.println("Dice 1: " + result.getLeft());
System.out.println("Dice 2: " + result.getRight());
object.rollDiceWithResultHandler((dice1, dice2) -> {
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
});
}
}
You don't need to create your own class to return two different values. Just use a HashMap like this:
private HashMap<Toy, GameLevel> getToyAndLevelOfSpatial(Spatial spatial)
{
Toy toyWithSpatial = firstValue;
GameLevel levelToyFound = secondValue;
HashMap<Toy,GameLevel> hm=new HashMap<>();
hm.put(toyWithSpatial, levelToyFound);
return hm;
}
private void findStuff()
{
HashMap<Toy, GameLevel> hm = getToyAndLevelOfSpatial(spatial);
Toy firstValue = hm.keySet().iterator().next();
GameLevel secondValue = hm.get(firstValue);
}
You even have the benefit of type safety.
Return an Array Of Objects
private static Object[] f ()
{
double x =1.0;
int y= 2 ;
return new Object[]{Double.valueOf(x),Integer.valueOf(y)};
}
In my opinion the best is to create a new class which constructor is the function you need, e.g.:
public class pairReturn{
//name your parameters:
public int sth1;
public double sth2;
public pairReturn(int param){
//place the code of your function, e.g.:
sth1=param*5;
sth2=param*10;
}
}
Then simply use the constructor as you would use the function:
pairReturn pR = new pairReturn(15);
and you can use pR.sth1, pR.sth2 as "2 results of the function"
You also can send in mutable objects as parameters, if you use methods to modify them then they will be modified when you return from the function. It won't work on stuff like Float, since it is immutable.
public class HelloWorld{
public static void main(String []args){
HelloWorld world = new HelloWorld();
world.run();
}
private class Dog
{
private String name;
public void setName(String s)
{
name = s;
}
public String getName() { return name;}
public Dog(String name)
{
setName(name);
}
}
public void run()
{
Dog newDog = new Dog("John");
nameThatDog(newDog);
System.out.println(newDog.getName());
}
public void nameThatDog(Dog dog)
{
dog.setName("Rutger");
}
}
The result is:
Rutger
You can create a record (available since Java 14) to return the values with type safety, naming and brevity.
public record MyResult(int number1, int number2) {
}
public static MyResult something() {
int number1 = 1;
int number2 = 2;
return new MyResult(number1, number2);
}
public static void main(String[] args) {
MyResult result = something();
System.out.println(result.number1() + result.number2());
}
First, it would be better if Java had tuples for returning multiple values.
Second, code the simplest possible Pair class, or use an array.
But, if you do need to return a pair, consider what concept it represents (starting with its field names, then class name) - and whether it plays a larger role than you thought, and if it would help your overall design to have an explicit abstraction for it. Maybe it's a code hint...
Please Note: I'm not dogmatically saying it will help, but just to look, to see if it does... or if it does not.