1

I really am a little confused here. Normal signature to call accessible class method or variable is (Class/Object).(method/variable). Then how do we give System.out.println()? Since System.out only gives the return type but does not belong to same class. Also in servlets, "this.getServletConfig().getInitParameter("defaultUser")" is not making sense to me, since getServletConfig() and getInitParameter are both member functions of same class, so signature becomes something like, class.method1().method2(), where method1 and method2 are member functions of same class. Can someone please explain..

Example:

Class CascMethodClassB() 
{ 
public CascMethodClassA methodTest()
{
    CascMethodClassA obj1 = new CascMethodClassA();
    return obj1;
}
} /*Class CascMethodClassB ends*/

Class CascMethodClassA() 
{
public int varTest;

public CascMethodClassA()
{
varTest = 7;
}

} /*Class CascMethodClassA ends*/


Class CascMethodClassC() 
{
CascMethodClassB obj2 = new CascMethodClassB();
int varTestC = obj2.methodTest().varTest

public static void main(String[] args)
{
System.out.println("varTest in CascMethodClassA is: "+ varTestC);

} /*Class CascMethodClassC ends*/

}

Thankyou, Fraggy.

3 Answers3

1
  • System.out is a public class variable of type PrintStream, not a method. Therefore you can invoke the println method on it, which returns void.
  • this.getServletConfig().getInitParameter("defaultUser") makes perfect sense once you understand chaining method invocations. In this case, you are:
    • calling the present instance of Servlet
    • getting its instance field's value of type ServletConfig
    • getting whichever String value is returned by invoking the getInitParameter method on the ServletConfig object
  • Finally, a method's signature is made of the method's name and parameter types
Mena
  • 47,782
  • 11
  • 87
  • 106
  • Hi Mena, so does that mean if return type of methodA of ClassA is Integer as well as it's variable, variableA and ClassC has a public method methodC of return type Integer, i can invoke methodC as something like: ClassA obj1 = new ClassA(); obj1.methodA.methodC() or obj1.variableA.methodC(); Thanku much for responding. –  May 26 '14 at 22:08
  • @user3595802 hard to say, it's not very clear from your question. As a rule of thumb, either edit this question with your code if pertinent (and compiles) or ask a new question. One thing for sure is that method invocation implies parenthesis, so it's either `obj1.methodA().etc...` or `obj1.methodA(whateverCommaSeparatedArguments).etc...`, or `obj1.variableA.etc...`. – Mena May 26 '14 at 22:13
1

Both are different cases.

In the first case, outis a public static member in the System class. The member out is of type PrintStream, so the call

System.out.println()

will call the method println() from the PrintStream object (out).

The second case, is something called method chaining. What happens is that class.method1() will return an object instance, according to Java docs it will return a ServetConfig object. So, you can again call a method from that returned object. Another way of seeing that call is (brackets are redundant, just there so you can visualize the order of the calls):

(ClassName.someMethod1()).someMethod2();
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • so does that mean if return type of methodA of ClassA is Integer as well as it's variable, variableA and ClassC has a public method methodC of return type Integer, i can invoke methodC as something like: ClassA obj1 = new ClassA(); obj1.methodA.methodC() or obj1.variableA.methodC(); Thanku sooo much for taking out time to answer. –  May 26 '14 at 20:52
  • additionally what do u think about String myPost = " http://lillbitofeverythingagain.blogspot.com/2014/03/takeaways.html " –  May 26 '14 at 21:18
  • @user3595802 Nope. You have to look at the **type of the result**. The result of `obj1.methodA()` is an `Integer`, and the `methodC()` belongs to type `ClassC`. The types doesn't match, so you can't call it. – Christian Tapia May 27 '14 at 04:56
  • I have edited my question and added a piece of code to explain method chaining. So let me summarize what i am getting : if return type of datamember is type object(example return type of "out" in "System.out" is of class object type "PrintStream") ), in such cases, you chain link them to access datamemembers of same return class. Yeah, it makes sense now. Thanks. Please let me know if the sample code i have added in question explains the scenario quite well or if its not correct. Thanks, Fraggy. PS: I tried directing this comment to you, but @Christian is not addressing. Sorry. –  May 27 '14 at 06:05
  • Yes, you only forgot a semicolon but the logic is OK. I think you've understood correctly. – Christian Tapia May 27 '14 at 06:10
  • Don't forget to mark the answer that helped you the most correctly. That way future visitors will know which answers to read. – Christian Tapia May 27 '14 at 06:14
  • o yes.. i want to mark others helpful atleast, but i guess that isnt possible. –  May 27 '14 at 06:19
1

Each non-void method returns a type, which may be a different type to the declaring class, so the chained method/field will have the methods of the returned type (not the class it's called from or the class that the first method is defined in).

For example, to break down System.out.printkln():

System.out // out is a public field of type PrintStream
.println() // println() is a method of PrintStream, not System
Bohemian
  • 412,405
  • 93
  • 575
  • 722