473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

a little confusion with my code .....

dmjpro
2,476 2GB
here is my code ....... plz look at carefully

class Test2
{
}

class Test1
{
void method(Test2 i)
{
System.out.println("Debasis: " + i);
}
}

class AnonymousTest
{
public static void main()
{
Test1 t = new Test1();
String str = "Debasis";
t.method(new Test2(){ public String toString(){return str;} });
}
}

now i get a compiler error ... the variable str needs to be declared as final

why this happens ... plz help....thanxxxxxx
Mar 12 '07 #1
7 1342
r035198x
13,262 8TB
here is my code ....... plz look at carefully

class Test2
{
}

class Test1
{
void method(Test2 i)
{
System.out.println("Debasis: " + i);
}
}

class AnonymousTest
{
public static void main()
{
Test1 t = new Test1();
String str = "Debasis";
t.method(new Test2(){ public String toString(){return str;} });
}
}

now i get a compiler error ... the variable str needs to be declared as final

why this happens ... plz help....thanxxxxxx
1.) dj you forgot the code tags again!
2.)You are accessing a local variable from an inner class therefore it must be declared final
Mar 12 '07 #2
dmjpro
2,476 2GB
again sorrry .....thanx for ur replyyyy

i know i trying to access the local varibale ... so it needs to be declared as final

but i want to know if the java compiler allows to do so then what would happen.....
i just want to know what is the logic behind it ....

plz help me .... thanxxxxx
Mar 12 '07 #3
r035198x
13,262 8TB
again sorrry .....thanx for ur replyyyy

i know i trying to access the local varibale ... so it needs to be declared as final

but i want to know if the java compiler allows to do so then what would happen.....
i just want to know what is the logic behind it ....

plz help me .... thanxxxxx
It's a memory allocation issue dj. When memory for the inner class object is reserved, everything about it needs to be known at that moment so that the best fit slot is selected for it. That's why the variable allowed for it must be final.
Mar 12 '07 #4
dmjpro
2,476 2GB
it is not still clear to me ..... if u can't explain me in one thread ... plz send me a good link which can describe in details...

plz help ..... thanx
Mar 12 '07 #5
r035198x
13,262 8TB
it is not still clear to me ..... if u can't explain me in one thread ... plz send me a good link which can describe in details...

plz help ..... thanx
Try this but you may need to read the JVM specification to fully understand this.
Mar 12 '07 #6
dmjpro
2,476 2GB
thanx a lot ... have a good day
Mar 12 '07 #7
dmjpro
2,476 2GB
hello frndsss ..... i got a good exlaination on it .... if can plz check it out



written by hiwa from java world


See: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3
An inner class, however it is declared and defined in a particular method, makes a separate object outside the method so the object can't access method local variables, which are allocated on the mthod's stack frame, directly. Thereof that enforces Java to pass a copy of local variable to the inner class object.

// str is a copy of local variable str!!!
t.method(new Test2(){ public String toString(){return str;} });

If the method happens to modify the value of the local var, the new value doesn't reflect to the copy in the inner class object -- that should invite a terrible disaster. In order to prevent it, local var referenced in an inner class must be final.
Mar 13 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
2
by: nobody | last post by:
Why doesn't the casting of "e.Item.DataI­tem" as a "DataRow" (or even a "DataRowView" as in other online examples) work? I would love to get this type of code to work with my code that uses a...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
14
by: Brett Sinclair | last post by:
Hello everybody I'm still on the learning curve here...and from what I read, I created inherited datagrid class so I could have icons, combobox...etc in the columns of my datagrid. The grid...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
5
by: ducky801 | last post by:
Hi all. When i run the code below i get a 'NullReferenceException was unhandled' error. I am using VB 2005 Express. I'm confused about this because, I have declared my array and i'm trying to...
5
dmjpro
by: dmjpro | last post by:
i konw that the static methods r not overriden.....right??? i think it is not. now my confusion is that ... to declare a static method as final ... this is meaning lesss..right??? but the...
5
dmjpro
by: dmjpro | last post by:
i know that the finalize method is called by JVM when an object is garbage collected. right??? but the ultimate memory release done by native OS. now my confusion is ... when it is called in...
18
by: miller.paul.w | last post by:
Ruby has a neat little convenience when writing loops where you don't care about the loop index: you just do n.times do { ... some code ... } where n is an integer representing how many times you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.