File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes java about pass by value,why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java about pass by value,why?" Watch "java about pass by value,why?" New topic

java about pass by value,why?

Jin chengong

Joined: Oct 29, 2007
Posts: 10
class Value{
public int i = 15;

public class Test{
public static void main(String args[]){
Test t = new Test();
public void first(){
int i =5;
Value v = new Value();
v.i = 15;
second(v,i); <<<<<here....
public void second(Value v,int i){
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i+" "+i);
the answer is 15 0
anyone who can tell me why?
Marvin Domingo

Joined: Dec 17, 2007
Posts: 22

I'm a beginner as well, about to start on chapter 5 or head first java, you're asking why 15 0 20 is the ouput?, maybe because of this method

I don't know how to explain it, but from the title of your post itself explains it (this is my way of learning, to interfere with some other beginners code )
[ December 23, 2007: Message edited by: Marvin Domingo ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Hello Buddies,

Perhaps, these links might help you.

  • Story PassBy
  • Pass by References for objects

  • Good luck!

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Jin chengong

    Joined: Oct 29, 2007
    Posts: 10
    thank you for you reply!
    i will take a look at!
    It is sorta covered in the JavaRanch Style Guide.
    subject: java about pass by value,why?
    It's not a secret anymore!