• Post Reply Bookmark Topic Watch Topic
  • New Topic

scope of reference variable  RSS feed

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,when does f1 stop pointing to its object?

my question is when control reaches line 5 is f1 still pointing to file object?
I know scope of local variable is within the method it is declared in,but still have this doubt.

Thank You
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 5 is outside the method the local variable was created in. Try this… and see whether you can get it past the compiler
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is gives compile time error,
but from context of JVM will f1 be still pointing to its object?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:yes it is gives compile time error,
but from context of JVM will f1 be still pointing to its object?


Are you asking, as part of the JVM implementation, will the memory address that used to be where the f1 variable was, is still pointing to the object? Perhaps, but does it matter? The variable is out of scope, so the memory is not in used... and hence, the object is no longer reachable from that (former) reference.

Henry
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after line 5 I am trying to delete the file using f2 reference,
on linux os it is deleted but on windows I get "unable to delete the file" exception.
so I was thinking if f1 still pointing to file created and is locked by same.

Thank you
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you reach line 5 of your code, f1 basically does not exist. As soon as you leave the createFile() method, talking about f1 doesn't make any sense. If your code looked like this:


Would you ask about f1 when you have only reached line 4? No - because f1 doesn't exist yet. In a similar fashion, when you reach my line 6, f1 doesn't exist, so asking what it points to makes no sense.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:after line 5 I am trying to delete the file using f2 reference,
on linux os it is deleted but on windows I get "unable to delete the file" exception.
so I was thinking if f1 still pointing to file created and is locked by same.


This is merely a behavior difference between Linux and Windows -- and has little (or nothing) to do with Java.

With Linux, when you delete a file, it is merely erased from the directory. However, the OS reference counts the resources, so if another program (or the same program) has the file open, the resources are not released until the last of the file accesses to the "file" are closed. In other words, you believe the file is deleted right away, but programs that are still using the file can continue to do so, until they close the file.

With Windows, basically, you are not allowed to delete a file when another program (or the same program) has the file open.

To solve the windows issue, you need to figure out who has the file open, and have them close it before your program can delete it.

Henry
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys i found out the problem.
i was facing this problem while using jpa-hibernate,the files stream was used to persist in database,
so i was unable to delete the file before commit happened.
now i have put the file deletion logic in another place and its working.

Thanks to all
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:guys i found out the problem.
i was facing this problem while using jpa-hibernate,the files stream was used to persist in database,
so i was unable to delete the file before commit happened.
now i have put the file deletion logic in another place and its working.

Thanks to all


Thanks for following up and reporting the source of the file open. Have a cow.

Henry
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank a lot Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!