Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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  
  objects existing beyond their scope  (Read 535 times)
0 Members and 1 Guest are viewing this topic.
Offline jasonworthing

Senior Newbie





« Posted 2014-05-29 17:46:33 »

As I understand it, an object or variable only continues to exist within the brackets that its created in. So if a create an int in a for loop, it will be destroyed outside of the loop. But what if I wanted it to exist beyond that. What if you would know outside of the for loop what types of variables would be created or how many because it depends on user input?   
Offline Kefwar
« Reply #1 - Posted 2014-05-29 17:49:11 »

Just declare them outside the for loop:
1  
2  
3  
4  
5  
int myint = 0;
for(int i = 0; i < 3; i++) {
  myint += i;
}
System.out.println(myint);
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2014-05-29 18:35:51 »

I don't understand... you seem to know how scope works, but then you seem to think you can get around it...?

If you create an object within brackets, its going to (in theory) be sent to the G.C. when its scope limit is reached, there's nothing you can do about that. You can declare the object in a "bigger" scope (outside of the brackets) and then you can keep using the object, or you can do something like this:
1  
2  
3  
4  
5  
6  
7  
int y;

for(int i = 0; i < 5; i++) {
   int z = i;
   y = z;
   //z's scope ends here, it will be "deleted". However, 'y' holds the value of 'z' so you can keep using it essentially.
}


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline saucymeatman
« Reply #3 - Posted 2014-05-29 18:37:59 »

Set a reference to the object in a larger scope.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Object globalObject;

{
Object o;
// create Object o

globalObject = o;
// globalObject now points at the Object o
}

o.doSomething();
// Do something with the Object o
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2014-05-29 18:39:07 »

That won't work Wink

o's scope ends after the brackets, you can't call upon it! You probably meant to type globalObject.

Offline Drenius
« Reply #5 - Posted 2014-05-29 18:44:46 »

(globalObject.doSomething();)
Offline jasonworthing

Senior Newbie





« Reply #6 - Posted 2014-05-29 19:04:00 »

I don't understand... you seem to know how scope works, but then you seem to think you can get around it...?

If you create an object within brackets, its going to (in theory) be sent to the G.C. when its scope limit is reached, there's nothing you can do about that. You can declare the object in a "bigger" scope (outside of the brackets) and then you can keep using the object, or you can do something like this:
1  
2  
3  
4  
5  
6  
7  
int y;

for(int i = 0; i < 5; i++) {
   int z = i;
   y = z;
   //z's scope ends here, it will be "deleted". However, 'y' holds the value of 'z' so you can keep using it essentially.
}



That basically was my question. I understood that I can (and should) just declare variables outside of the for loop (or whatever brackets we're talking about) in order to keep it in scope. I was just wondering if there was a way around it.

Thanks though, that answers my question!
Offline HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2014-05-29 19:59:19 »

Note: Scope and lifetime are not the same thing.

Offline Sickan

Senior Duke


Medals: 9



« Reply #8 - Posted 2014-05-30 00:28:39 »

Note: Scope and lifetime are not the same thing.
Clarification: scope is the amount of time you can count on the object existing - AKA how long it exists theoretically. Lifetime is how long is practically exists before it's garbage collected.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!