Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by ref- pgm err

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having doubt in Pass by Value and Pass By Reference in Java. So I tried to run the following pgm. It displaying error:



Error Message:
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void changeObjectRef(MyDate ref)

{
ref = new MyDate(1, 1, 2000);
}
public static void changeObjectAttr(MyDate ref)
{
ref.setDay(4);
}

Your method should define the argument "ref" of type "date" and not "Mydate".
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try changing all your MyDate to Date.
Moreover Date(int year, int month, int date) is deprecated.
Try using java.util.Calendar;
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having doubt in Pass by Value and Pass By Reference in Java.
There's nothing to doubt; Java is always pass-by-value. Pass-by-reference is not possible in Java.

(Don't let that stop you from experimenting, though...)
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I imported java.util.calendar; and replaced MyDate with Date. Still it's pointing out the same error.

Error
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is case sensitive: java.util.calendar is not java.util.Calendar.

Second, in order to use Date, you have to import java.util.Date, not java.util.Calendar.

(Thirdly, I never import java.util.Date anyway, since the package name is short and java.util.Date can be confused with java.sql.Date, so I always spell them out anyway...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic