Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Constructors w/ Objects as parameters?  (Read 1455 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob Pickens
« Posted 2014-06-02 01:03:52 »

Say you have an object in you constructor like this:

1  
2  
3  
Thing(Object o){
      this.o = o;
}


Now say that a variable in the object passed into the constructor on creation of an instance of the thing class. Does the object in the instance of the thing class get an updated variable too?

What I'm trying to say is when an object is passed into a constructor does it just clone that object over and then its done, or does it stay up to date with the object passed in?

Does this make sense?

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-06-02 01:10:15 »

The more or less definitive answer: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

The short answer: No, it isn't cloned,
this.o
(the field named "o" of that Thing instance) holds a pointer to the object you gave the constructor.
You can write a simple test to prove it.
Offline Jacob Pickens
« Reply #2 - Posted 2014-06-02 01:10:50 »

Awesome! Thanks for the info! +1

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2014-06-02 01:26:53 »

everything in java is a pointer, everything but the primitives

even Object blah = new Object(); is a pointer. The VM creates a new Object somewhere on the heap and the blah variable points to it.
cloning never happens unless you specifically program it. no new objects/clones are created if you dont use "new"

when using primitives that doesnt apply
int b = 4;
r = b; //makes a copy

Offline trollwarrior1
« Reply #4 - Posted 2014-06-02 04:15:01 »

If you want to copy an object, you can use a protected method clone()
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (28 views)
2019-09-04 15:33:17

hadezbladez (3957 views)
2018-11-16 13:46:03

hadezbladez (1434 views)
2018-11-16 13:41:33

hadezbladez (3954 views)
2018-11-16 13:35:35

hadezbladez (765 views)
2018-11-16 13:32:03

EgonOlsen (4079 views)
2018-06-10 19:43:48

EgonOlsen (4648 views)
2018-06-10 19:43:44

EgonOlsen (2748 views)
2018-06-10 19:43:20

DesertCoockie (3643 views)
2018-05-13 18:23:11

nelsongames (3834 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!