• 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

Pass by ref- pgm err

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...)
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic