2

Really want to initialise some arguments in a Java method eg

public void delete(String name, int user_id=0) { //method body }

so that the user_id does not have to necessarily be passed. But i'm getting this Eclipse error that the token is invalid. How do i go about this?

2 Answers 2

13

Java != PHP.

But you can write:

public void delete(String name){
  delete(name, 0);
}

public void delete(String name, int user_id){/* ...*/}
Sign up to request clarification or add additional context in comments.

2 Comments

oops! i think my brain is still not healed from the PHP infections. I wanted to avoid having many methods..but its ok
There is no way in java to have a default parameter. The only way is to have multiple methods.
0

You can uses generic bounded methods. I kind of think they are messy and only recommend it if you actually have a deed to do it.

https://docs.oracle.com/javase/tutorial/java/generics/boundedTypeParams.html

I've found youtube videos on how to do this and it does work. But like I said, personally if it is only 2 or 3 methods you are making I would make the methods, cause this is messy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.