• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Static Method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys , Confusion ....confusion ...Confusion .

This is a mockup Question .

class Practice{

public void method(int arr[]){
arr[0] = 10;
}
public static void main(String[] args){
int[] small = {1,2,3};
Practice t= new Practice10();
t.method(small);
System.out.println(small[0]);
}
}

When I saw this question the first thing that came to my mind is the fact that Non -Static methods cannot be reference from static context .

So I thought this would result in a compile time error . But when I executed the program it executed correctly . Ok that was my first question .

I was thinking of the output for this program .

I thought coz in Java method calling is by "pass by value" , I thought after the method call t.method(small); the output would be 1 but to my surprise the answer was 10 .

Can anyone please clear my doubts ? I would really appreciate your help .

Regards,
Rakesh
[ October 05, 2007: Message edited by: Rakesh Yelugoila ]
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all check the code. You have Test10 t= new Test10();
but class is called Practice, they don't match.

Non -Static methods cannot be reference from static context .
That is true, but you got to read the fine print: except through instance variables. Then you can!

Yes method calling is always "pass by value". But you got to twist your thought a bit on this one. For primitives like byte, short, int, long, boolean, char, a copy is made and passed in. So the original is not touched. HOWEVER, when you are passing in an reference/pointer to an object. A copy of the reference/pointer is passed, not a copy of the whole object is passed. You got to read this twice and think about it. There is only one ojbect, and no additional object is created. Only additional reference/pointer is created when passed into the method.

So since that additional reference is pointing to the same object, you can actually change the value. That's why arr[0] = 10;
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was a good one! Helped me too!
 
Rakesh Yelugoila
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony for clearing my confusion . I really appreciate your help .



Originally posted by Tony Smith:
First of all check the code. You have Test10 t= new Test10();
but class is called Practice, they don't match.

Non -Static methods cannot be reference from static context .
That is true, but you got to read the fine print: except through instance variables. Then you can!

Yes method calling is always "pass by value". But you got to twist your thought a bit on this one. For primitives like byte, short, int, long, boolean, char, a copy is made and passed in. So the original is not touched. HOWEVER, when you are passing in an reference/pointer to an object. A copy of the reference/pointer is passed, not a copy of the whole object is passed. You got to read this twice and think about it. There is only one ojbect, and no additional object is created. Only additional reference/pointer is created when passed into the method.

So since that additional reference is pointing to the same object, you can actually change the value. That's why arr[0] = 10;

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It realy helps............

Dammm Gud Explanation....
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic