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 6 years ago.
Improve this question
I am asked to say what does this code do but I really cannot figure it out.
I've tried to execute it in netbeans and got the answer 6 but really cannot understand why.
public class Quattro {
int x = 5;
Quattro s = this;
Quattro f(){
s.s.s.x++;
return s;
}
void g(){System.out.println(x);}
public static void main (String[] args){
Quattro a4 = new Quattro();
a4.f().g();
}
}
Question 1: What does Quattro s = this; do? Am I declarind a pointer to my self? If so, it means that I can write
Quattro f(){
s.s.s.x++;
return s;
}
or even
Quattro f(){
s.s.s.s.s.s.s.s.x++;
return s;
}
and I'll always get the same result because I'm in a loop?
Question 2: I do not understand what a4.f().g(); does... seems so weird to me.
If you assign this reference to a member variable, you have a recursion. Yes, it doesn't matter how many s's you'll add, because they are always the same object, which is this object. It's the same as if you wrote:
this.this.this.this.this.this.x++;
Function f() returns reference to this object after doing some other operations on it. It's a common design pattern in Java, called builder. Adding ability to do a4.f().g(); to a class is called method chaining. In other words, f() is this object at the end of the call, just like s is, so you can do:
a1.f().f().f().f().f().f();
And it means you just called f() function from a1 object 6 times.
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 3 years ago.
Improve this question
So i wrote a method that accepts any java object and i figured out that
public void mymethod(Object javaobject) {
}
works, but with
public void mymethod(Object[] javaobject) {
}
Eclipse trows an error
The method mymethod(Object[]) in the type myClass is not applicable for the arguments (Object)
So my question is, where is the difference between these two types ?
Simply, the difference is that Object is a single object whereas Object[] is an array (multiple or collection) of indexed objects.
For example you could have an object that contains a string like
Object obj = "Hello";
or you could have an array of strings like
Object[] objArray = new Object[2];
objArray[0] = "Hello,";
objArray[1] = " world!";
So, obj is one Object. Whereas, objArray is an array that contains multiple objects indexed starting with 0. Hopefully this helps!
An array is-a an Object, but one Object is not (necessarily) an array of Object(s). You could make your method take varargs,
public void mymethod(Object... javaobject) {
// ...
}
Then it would work like an array (that is javaobject is an array) in both cases (called with one, or more, Object(s)).
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 5 years ago.
Improve this question
We want each method to do a distinct task, right? How do you draw that line?
For example, say I have a class with an int array that I need to have set to certain numbers when the class is constructed. Should I loop through the array right there or make a separate method for what is ultimately a simple task?
Make a new method whenever you have a sensible name for one. When you have a good name for a new method, it suggests that what you're doing there is a separate task and potentially reusable.
Note that this is just a rule of thumb and may not apply to all cases. Another rule is to make a new method if your current method is too long (I've heard 48 lines cited as an upper bound).
Here is a sample class that I hope points out what you are looking for:
class Sample {
private int[] myInts = null;
// we need a constructor if we are going to pass in stuff
// if we don't provide a constructor, java will create one for us
public Sample(int[] inputs) {
// I can just set this array, I don't need a separate method to
// loop through and create a new array and copy the old one.
myInts = inputs;
}
// here we are going to do something discreet, so I need a new method.
public int add() {
int returnValue = 0;
for (int i = 0; i < myInts.length; i++) {
returnValue += myInts[i];
}
return returnValue;
} // end my "add()" method
} // end my Sample class
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 8 years ago.
Improve this question
I am quite new to java and I have two questions!
First how can I make a variable usable entire a java class?
This is part of my method:
public void createID() {
ObjectId Id = (ObjectId) documentClient.get("_id");
StringID = clientId.toString();
}
And I want to use StringID in another method but it is either null (when I create public StringID =null in the main class) or not known.
My other question is that when I change the createID() to
public String createID() {
ObjectId Id = (ObjectId) documentCleint.get("_id");
StringID = clientId.toString();
return Id;
}
It says that the method should be void! I am looking for a way to return the variable from my method.
Any idea?
Thanks
First of all, in order to define a class variable, you should define it under the class definition. For example
public class Example{
private int classVariable; // Variable to be reached from any method in class.
private void someMethod(){
}
}
In your clientID method, you assigned return type as String, but you are returning objectId. I guess you meant to return StringID variable.
Also I guess, documentCleint.get("_id");
should have been documentClient.get("_id");
You should try after correcting these syntax issues.
Question 1 :
First how can I make a variable usable entire a java class?
That createID limited to the scope of createID() method. To avail that in whole class, move them to top. (instance members).
Question 2 :
It says that the method should be void!
public **String** createID() {
ObjectId **Id** = (ObjectId) documentCleint.get("_id");
StringID = clientId.toString();
return **Id**;
}
That is not the exact error. Return type is mistamatch there. That method should return a String but you are returning Id which is of type ObjectId. You might want to return StringID
To use a variable in more than one method you need to use a global variable, to declare it simply declare it outside any method inside the class.
As for the second part of your question, it looks to me that you are not returning a string as id is not declared as a string
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This method has a non void return yet it does not use the return statement at all.
Can someone explain whats happening here?
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_oceans,true),
new TrueFalse(R.string.question_mideast,false),
new TrueFalse(R.string.question_africa,false),
new TrueFalse(R.string.question_americas,true),
new TrueFalse(R.string.question_asia,true),
new TrueFalse(R.string.question_asia,true),
};
It is not a method. It is a variable declaration. You create and initialize a TrueFalse array and assign it to the variable mQuestionBank
It is not a method, it is a field which is directly initialized
Example:
class IHaveAnInitializedField {
RandomClass a = new RandomClass();
public RandomClass getA() { return this.a; }
}
It is not a method it is an array declaration with initialization.
This code is trying to create TrueFalse[] array named mQuestionBank , where initilize 6 TrueFalse objects. It is not a method.
You are creating an array of 6 TrueFalse objects
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 8 years ago.
Improve this question
Well, i was thinking about that kind of methods.
They dont recive parameters, but they work with them.
An example: .replace(Char, Char) of String API, it works with a String followed by a dot.
Like:
String test = "= Text = without = equals";
String output = test.replace("=","");
How it works without receive the parameter test?
I'm just being curious, want to do a method like these.
Sorry for my bad english!
Thanks.
Let's say you build a new type of String, that only does replace stuff (why not?!):
public class MyString {
private final String s;
public MyString(String s) {
this.s = s;
}
public String replace(String search, String replace) {
return s.replace(search, replace);
}
}
Now you can call it like this:
MyString myString = new MyString("= Text = without = equals");
String output = myString.replace("=", "");
Et voila, you have done the same "trick"! And you can see how it works: your MyString object keeps some data internally (the s variable) and can access that data from its methods.
These are instance methods, which means that they require an instantiated class object to operate. So, the string class knows the value of test in your example, because it has access to the instantiated data.
To create these types of methods, simply define them within your class and do not mark them as static.
String is an object. The class definition is part of the Java standard library. That definition includes the replace method.
Since String is a final class you can not subclass it to add additional methods. So you can not do what you are trying to do.