This question already has answers here:
Can someone explain a void return type in Java?
(5 answers)
Closed 7 years ago.
I get the following error in the print method
Void methods cannot return a value
how to correct the code in method print?
abstract class Motor{
int fuel;
int getFuel(){
return this.fuel;
}
abstract void run();
}
public class Player extends Motor {
void run(){
print("wroooooom");
// public static void main(String[] args) {
}
private void print(String string) {
return string; // Void methods cannot return a value
}
}
You have written the following code:
private void print(String string) {
return string;
}
But void here is the return type. It thus means you cannot return anything. If you write String instead of void then it will mean that the return type will be a string which is the case and the error will go away.
It should be like this:
private String print(String string) {
return string;
}
Or as it is a print function if you want to keep it void then print the string there itself.
As requested in comment here is how the code should be. The main
function should be inside Player class. Then we define an object of
the Player class in the main function to call its methods.
abstract class Motor{
int fuel;
int getFuel(){
return this.fuel;
}
abstract void run();
}
public class Player extends Motor {
public void run(){
print("wroooooom");//calling print method to print passed string
}
public void print(String string) {
System.out.print(string);
}
public static void main(String []args){
Player p1 = new Player();//creating a object of Player class to access its methods
p1.run();//calling the run method
}
}
Related
This question already has answers here:
Java - get the current class name?
(12 answers)
Closed 3 months ago.
Lets take this for Example:
public class Vehicle {
public enum Car {
CAR1,
CAR2,
CAR3,
CAR4,
}
public enum BIKE {
BIKE1,
BIKE2,
BIKE3
}
}
public class Main {
public static void main(String args[]) {
Vehicle.Car value1 = Vehicle.Car.CAR1;
Vehicle.Bike value2 = Vehicle.Bike.BIKE1;
print(evaluateType(value1));
// Expected Output: Car
print(evaluateType(value2));
// Expected Output: Bike
}
}
Now the use case here is, We have to write the function evaluateType. I was wondering if there is a way to know the type of Enum we are using, if it is of type Bike or Car.
Given the fact the enums are stored as Int in memory, this doesn't seems like it can be done. But looking forward to any suggestion on how this type of situations could be handled.
Thank you.
Not sure if that is what you want but:
public class BaseClass {
public enum PropertyTypeString {
STRING1,
STRING2,
STRING3,
STRING4,
STRING5,
}
public enum PropertyTypeInt {
INT1,
INT2,
INT3
}
}
public class Main {
public static void main(String args[]) {
BaseClass.PropertyTypeString value = BaseClass.PropertyTypeString.STRING1;
BaseClass.PropertyTypeInt value_2 = BaseClass.PropertyTypeInt.INT1;
evaluateType(value);
evaluateType(value_2);
}
public static void evaluateType(Enum value)
{
System.out.println(value.getClass());
}
}
You can attach values to enum, you don't need to stick with basic enum :
public enum Element {
H("Hydrogen"),
HE("Helium"),
// ...
NE("Neon");
public final String label;
private Element(String label) {
this.label = label;
}
}
source : https://www.baeldung.com/java-enum-values
THIS IS MY MAIN CLASS:
public class App {
public static void main(String[] args){
Student s1=new Student();
};
};
THIS IS THE CREATED CLASS:
class Student {
public static void f1(){
f2();
}
public static String f2(){
return "hello";
}
public Student(){
f1();
}
}
Now , as i have created an object s1 in main class, the constructor is called ,which has f1() , so f1() is called , now f1() has f2(), so f2() is called , so i think "hello" must be printed but the output is not printed at all(nothing is printed). Can anyone please explain what the reason could be?
There is a difference between printing and returning a value.
If you want it to get printed, you should try doing something like this:
class Student {
public static void f1(){
f2();
}
public static void f2(){
System.out.print("hello");
}
public Student(){
f1();
}
}
f2() is returning the String, but f1 is not printing it:
public static void f1(){
System.out.println(f2());
}
public static String f2(){
return "hello";
}
public Student(){
f1();
}
To be printed at the Console Log you should try: System.out.println("Hello");
You are returning the value not printing it.
You have to use System.out.println("Hello") instead of return "hello";
I method....
Since the f2 method has a return type, in order to get the value obtained from it, make a reference of the type that is compatible with the return type and write the word hello using the same reference code as follows
class Student {
public static void f1(){
String x=f2(); //method calling
System.out.println(x);
}
public static String f2(){
return "hello";
}
public Student(){
f1();
}
}
II method......
You can try this way...
class Student {
public static void f1(){
System.out.println(f2());//calling method
}
public static String f2(){
return "hello";
}
public Student(){
f1();
}}
Is it possible to overload Enum abstract method?
I have tried this in my code with no effect.
Presented class
public class Test {
public void test(String string){
System.out.println(string);
}
public void test(Object object){
System.out.println("Test1");
}
public static void main(String[] args) {
Object object = new Object();
test.test(object);
test.test("what if?");
}
}
gives expected result of
Test1
what if?
while enum
public enum TestEnum {
TEST1{
public void test(String string){
System.out.println(string);
}
public void test(Object object){
System.out.println("Test1");
}
},
TEST2{
public void test(Object object){
System.out.println("Test2");
}
};
public abstract void test(Object object);
}
public class Test {
public static void main(String[] args) {
Object object = new Object();
TestEnum.TEST1.test("what if?");
TestEnum.TEST1.test(object);
}
}
returns
Test1
Test1
Is it even possible to overload Enum methods or am I doing something wrong? Or maybe should I check for type inside of overriden method and then act accordingly? But then I remove switch statement only to introduce another switch statement.
The thing about enums is that values with bodies are implemented as anonymous subclasses of TestEnum; so they look like this:
final TestEnum TEST1 = new TestEnum() { /* body */ };
Whilst the concrete class of TEST1 is, say TestEnum$1 (or whatever name the compiler decides to give it), the reference is of type TestEnum, so any code outside the body of TEST1 can only access methods defined on TestEnum.
Yes is possible, you are somehow not implementing that in a particular way....
you should
define an interface with the methods you want to override
interface Ifoo {
public void test(Object object);
public void test(String object);
}
then remove the abstract method of the enum and make the enum implement that interface, but override those methods in every constant of the enumerator...
enum TestEnum implements Ifoo {
TEST1 {
#Override
public void test(String string) {
System.out.println(string);
}
#Override
public void test(Object object) {
System.out.println("Test1");
}
},
TEST2 {
#Override
public void test(Object object) {
System.out.println("Test2");
}
#Override
public void test(String string) {
System.out.println(string);
}
};
}
finally implement it like>
Object object = new Object();
TestEnum.TEST1.test("what if?");
TestEnum.TEST1.test(object);
TestEnum.TEST2.test("why not?");
TestEnum.TEST2.test(object);
your result should looks like:
what if?
Test1
why not?
Test2
You are showing an example with a class and then you are showing an example with an enum. I believe you think these examples are equivalent, however, they are completely different each other.
For the example of your class to be equivalent to the example of your enum, you should modify your Test class so that it extends an abstract AbstractTest class:
public abstract class AbstractTest {
public abstract void test(Object object);
}
public class Test extends AbstractTest {
public void test(String string) {
System.out.println(string);
}
#Override
public void test(Object object) {
System.out.println("Test1");
}
}
Now, if you try the same lines you've tried in your first main:
AbstractTest test = new Test();
Object object = new Object();
test.test(object);
test.test("what if?");
You'll notice that the output has now become:
Test1
Test1
Which is something to be expected, because Java doesn't provide a feature called dynamic dispatch. Informally, dynamic dispatch means that the overloaded method to be executed is decided at runtime, based on the polymorphic types of the parameters. Instead, Java decides the method to be executed at compilation time, based on the declared type of the object whose method is to be invoked (in this case AbstractTest).
With enums, this is exactly what happens. All the elements of the enum (TEST1 and TEST2 in your example) belong to the type of the enum (TestEnum in your case), so the compiler always goes for the method that is declared as abstract.
The reason why you get twice "Test1" is because you have declared only this method
public abstract void test(Object object);
Precisely, this method will "catch" all calls whit any type of parameter. String extends Object (indirectly), so String is Object and this method we be called.
In other words, method wich receives parameter String will be hidden by the method which receives parameter Object.
The solution is to add next method declaration in enum
public abstract void test(String string);
You will have to add the implementation of this method to TEST2 constant.
Code
public enum TestEnum {
TEST1 {
public void test(String string) {
System.out.println(string);
}
public void test(Object object) {
System.out.println("Test1");
}
},
TEST2 {
public void test(Object object) {
System.out.println("Test2");
}
#Override
public void test(String string) {
// TODO Auto-generated method stub
}
};
public abstract void test(Object object);
public abstract void test(String string);
}
This code gives output
what if?
Test1
I am trying to use the String returned by this method in another class.
This is the method:
public String toString(){ return String.format("(%f, %f, %f)",longitude, latitude, elevation);}
This is the other class method:
void addPoint(){
coordinates.add(Point.toSting);
}
I know that I could create an object, but neither of these classes has a main method.
Are you looking for something like this?
Your Main Class which runs everything
public class Test {
private void run() {
new Test2();
}
public static void main(String[] args) {
Test t = new Test();
t.run();
}
}
Some Class which gets string from another class
public class Test2 {
private void displayString() {
System.out.println(new Test3().toString());
}
Test2() {
displayString();
}
}
Final Class which contains String
public class Test3 {
public String toString() {
return "Hi";
}
Test3() {
}
}
I'm trying to pass a string when creating a new instance of my Palindrome class but it keeps throwing errors at me, Any help?
public class Palindrome {
public String input;
public void Palindrome(String stringinput){
this.input = stringinput;
}
}
public class PalindromeTest {
public static void main(String[] args) {
Palindrome p = new Palindrome("test"); //i get an error here
}
}
If you're trying to define a constructor, this
public void Palindrome(String stringinput){
should be
public Palindrome(String stringinput){
Otherwise it is considered a method with a return type of void.
The void keyword tells the compiler that this is a method signature, not a constructor.
public void Palindrome(String stringinput){
this.input = stringinput;
}
The Palindrome class doesn't have an argumented constructor. It has only default non-arg constructor. I think you have mistaken to add void to public void Palindrome(String stringinput){} method. Because, you have made it as a method not as a constructor.