This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Compilation error after moving class to a separate .java file.  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a compile error after moving a class to a separate java.file.
Everything worked fine when the class was in the same file.

I made some variables protected and extended the main class in the separate class.

Eclipse me gives me error on this line from the “main” class:



And error on this line in the new .java file.



Can anyone see what's wrong? Like i said it worked when it was in the same file.
 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not without your stacktrace, but simply bassed on your actions, my guess is you haven't imported the file
 
Bartender
Posts: 5635
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a lot of info to go on.
Does the new file name match the class name?
Is the class declaration public?
Are the two classes in the same directory?
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Not without your stacktrace, but simply bassed on your actions, my guess is you haven't imported the file


I created the new .java file in the same project folder.

 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's some kind of memoryleak that causes a stackoverflow.
For the future: when you have a stack overflow, you have to look for a repeating patern in the call stack.
The call stack you're providing cuts off before any such pattern can be detected.
Obviously, you cannot provide a complete call stack, but something longer would have been more helpful
My guess would be this:

is causing the error
 
Carey Brown
Bartender
Posts: 5635
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielx Anderssonx wrote:

According to the stack trace you have an infinite recursion that goes until you have exhausted the stack memory. Line 86 in Gui.java calls  Gui.initialize, calls AddListenerShop and the back around to Gui line 86.
 
Carey Brown
Bartender
Posts: 5635
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that it is in
borderlayoutdemo.AddListenerShop.<init>(AddListenerShop.java:15)
which is constructing a new Gui object -- it shouldn't.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:My guess is that it is in
borderlayoutdemo.AddListenerShop.<init>(AddListenerShop.java:15)
which is constructing a new Gui object -- it shouldn't.


Probably. So maybe I should not extend the main class in the new .java file?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol... No you shouldn't... Why would you do that?
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:lol... No you shouldn't... Why would you do that?


To give the new .java file access to the variables in the main class?
If i leave everything in the same file it works so this is confusing.  
 
Marshal
Posts: 63391
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Anderssoon wrote:. . . To give the new .java file access to the variables in the main class? . . .

No, that sounds like bad design. Each object should have access to its own variables. I know private acccess also allows two objects to access each other's fields, but that is rarely a problem.
Also, why do you have a concept of  a main class? Each class is independent, and there shouldn't be one more important than all the others. Remember that the main() method only exists to start the application.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Daniel Anderssoon wrote:. . . To give the new .java file access to the variables in the main class? . . .

No, that sounds like bad design. Each object should have access to its own variables. I know private acccess also allows two objects to access each other's fields, but that is rarely a problem.
Also, why do you have a concept of  a main class? Each class is independent, and there shouldn't be one more important than all the others. Remember that the main() method only exists to start the application.


I solved the problem. I did not create new variables within the new class.
Instead i followed eclipse's advice and made the variables static and created "getters and setters".

I have heard before that would be bad design. But I don't yet understand why you would want to create new variables in every class if every class share and rely on the same variable if that makes any sense.  
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why making them static if you provided getters?
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Why making them static if you provided getters?


I wondered that myself. That was what eclipse told me to do.
Thanks for clarifying that.  
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was ment as eather/or, but making them static is indeed like you already poiinted out... Bad design
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!