• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

My code doesn't have any error but when I run it doesn't do anything

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem with my code in Eclipse IDE for Java 2020 where when I put my code to run, it doesn't do anything, I am doing a online course but I cant ask question, because the class are recorded before (3 years ago)
Some of the things on the code is in Brazilian Portuguese, but the things that are doesn't matter, they are only class notes, here is my code:



Thanks for the help
 
Rancher
Posts: 549
8
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luis Felipe, can you explain what you mean by doesn't do anything? Is the console displayed empty or program can't run at all?
 
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem I see is the code uses == to compare String objects.  You should use the equals() method to compare the contents of String objects.
 
Greenhorn
Posts: 4
1
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have everything commented in the main method so i assume that you are asking about part below. You have to make variable and initialization static in order to run it without creating object of your class:

Otherwise you have to create object of your class in your main method, and keep everything as it is:
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maciej Liso wrote:. . . . You have to make variable and initialization static . . .

Afraid that is incorrect. The correct object‑oriented way to do things is to get into a context of using objects as soon as posssible; things static don't constitute part of an object.

LS: welcome to the Ranch Thank you for trying the code button,  but you didn't quite get it right: put [/code] after the block. I corrected it for you and doesn't it look better. Unfortunately I can no see some poor indentation which can cause you problems if anything goes wrong with your flow of execution.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, it might be the curly brace on line 48 that terminates the main method.
 
Norm Radder
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To have the code in the static code block (starts at line 48) be executed, there needs to be a call to the Main class's constructor in the main method.
Add: new Main(); after line 4
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GZ: welcome to the Ranch

Norm, please explain a little more about what a static initialiser might not be executed.
 
Norm Radder
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I misspoke.  The code does not have a static block. I guess the term I meant is a code block.  I think a code block outside of any methods is sort of like a constructor that is executed when a new instance of the class is created.

This site has some details: https://www.geeksforgeeks.org/order-execution-initialization-blocks-constructors-java/
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an instance initialiser in that case. It never runs until an instance is created, and if I remember correctly, it behaves as if it were copied into the beginning of the constructor just after super(...); (or this(...);). The gory details to prove me wrong are in the Java® Language Specification (=JLS).
 
Gertjan Zwaveling
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me the code looks like extended Hello World so to get it to run the easiest way is to include it in the main by removing the braces on line 48 and 49. Maybe later in his career Luis Felipe can learn all about instances and static things but I guess it's too early for that.
reply
    Bookmark Topic Watch Topic
  • New Topic