• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Pass by ref- pgm err  RSS feed

 
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:
 
Ranch Hand
Posts: 662
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: 662
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;
 
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
 
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...)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!