• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double code block, Picking variables  RSS feed

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. Why 'class Selection' demands double code blocks?
2. How to pick-up 'String result' and use it in class 'Leans' ?
   
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 20 - 32 have to be contained within a method block.
 
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

Regardless of whether the code is in a method, or in an instance initializer (currently), the result variable would still be a local variable in both cases -- and would not be available to the main() method.

Henry
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not called a "double code block".  What you have created is an instance initializer.  See this page: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html - the last section, "Initializing Instance Members" explains what you have stumbled into.

Normally, code like that will be found in the constructor.  If you don't want it executing immediately when an instance is created, then you'd put the code in a method, as Carey suggested, and call it when you want it to be executed.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
bob john
Ranch Hand
Posts: 116
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Code works fine now.
In case If I decide to keep method with a loop 'for'. There compilator marks as mistake- method's  'bracket closure' and demands 'return null' to be added. Why it is so?
 
Sheriff
Posts: 4930
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terrible code formatting and indentation 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!