orphan code lines

V

V S Rawat

Of a piece of code is written after the closing } of
previous method and before the { of the next one, when will
it get executed, if ever.

When will the "menu things" will get assigned in the
following description, mentioned after c2 and before c3?

-Rawat
---------------
class C1 extends JPanel implements ActionListener,
DocumentListener,
MouseListener, KeyListener {

code....
public static void main (String[] args) {
code....
}

public C1 () {
code....

} // end of constructor

private void c2 () {

} //c2

private String[] menubar_info;
private JMenuBar menubar = new JMenuBar();
private JMenu menu1 = new JMenu();
private JMenu menu2 = new JMenu();
private JMenu menu3 = new JMenu();

private void c3 () {

} //c3

-----------------------------
 
V

VisionSet

Of a piece of code is written after the closing } of
previous method and before the { of the next one, when will
it get executed, if ever.

When will the "menu things" will get assigned in the
following description, mentioned after c2 and before c3?
....
private void c2 () {

} //c2

private String[] menubar_info;
private JMenuBar menubar = new JMenuBar();
private JMenu menu1 = new JMenu();
private JMenu menu2 = new JMenu();
private JMenu menu3 = new JMenu();

private void c3 () {

} //c3

-----------------------------

Before c2.
All attributes are initialised in the order they appear irrespective of any
method declarations.
Any method that is called on an object will do so on an object that has all
its attributes initialised, even if this is an implicit initialisation such
as:

class X {
int a;
}
 
V

V S Rawat

VisionSet said:
Of a piece of code is written after the closing } of
previous method and before the { of the next one, when
will it get executed, if ever.

When will the "menu things" will get assigned in the
following description, mentioned after c2 and before
c3?

....

private void c2 () {

} //c2

private String[] menubar_info; private JMenuBar
menubar = new JMenuBar(); private JMenu menu1 = new
JMenu(); private JMenu menu2 = new JMenu();
private JMenu menu3 = new JMenu();

private void c3 () {

} //c3

-----------------------------


Before c2.

does that mean "before the first method in the file/class"?
All attributes are initialised in the order they appear
irrespective of any method declarations. Any method that
is called on an object will do so on an object that has
all its attributes initialised, even if this is an
implicit initialisation such as:

Seems correct explanation. It is indeed getting initialized
and the elements are appearing on screen.

talking about good coding practices, isn't it more
appropriate to code it within a method or in the constructor?

I had found that in a package downloaded from net. I shall
try to move these lines before the first call to anyone of them.
class X { int a; }

-- Mike W

thanks.
-Rawat
 
V

VisionSet

V S Rawat said:
does that mean "before the first method in the file/class"?

Yes, it doesn't make sense any other way. Methods need never be called on an
object, but the constructor may need all the attributes.
Seems correct explanation. It is indeed getting initialized
and the elements are appearing on screen.

talking about good coding practices, isn't it more
appropriate to code it within a method or in the constructor?

Code what within the constructor?
I had found that in a package downloaded from net. I shall
try to move these lines before the first call to anyone of them.

Not neccessary, it depends on the logical ordering of class elements, for
instance sun usually put all there look and feel delegation code at the end
of a swing component class, whether that is attributes or methods. But for
most classes I imagine it makes sense to have them at the start.
 
V

V S Rawat

VisionSet said:
Code what within the constructor?

I meant that such orphan lines should not be left orphaned.
they should be kept within some method or in constructor, or
somewhere which relates them to where they are going to be used.

-Rawat
 
V

V S Rawat

VisionSet said:
Code what within the constructor?

I meant that such orphan lines should not be left orphaned.
they should be kept within some method or in constructor, or
somewhere which relates them to where they are going to be used.

-Rawat
 
V

VisionSet

V S Rawat said:
I meant that such orphan lines should not be left orphaned.
they should be kept within some method or in constructor, or
somewhere which relates them to where they are going to be used.

No it is fine to declare and initialise on one line.
A valid alternative is to initialise within a constructor.
But it is impossible to initialising an attribute within a method, since it
will already have been done.

Please don't post html to newsgroups.
 
V

V S Rawat

VisionSet said:
Please don't post html to newsgroups.

No I am not.

I have netscape and I compose only in text. all my other
mailing lists and ngs are getting my mails and replies in text.

how come?

-Rawat
 
V

VisionSet

V S Rawat said:
No I am not.

I have netscape and I compose only in text. all my other
mailing lists and ngs are getting my mails and replies in text.

how come?

Sorry my mistake, my newsreader is doing funny things with your encoding.
 
C

Chris Smith

V said:
I meant that such orphan lines should not be left orphaned.
they should be kept within some method or in constructor, or
somewhere which relates them to where they are going to be used.

The word "orphan" here implies that there's something wrong. There
isn't. It perfectly fine to use initializer expressions when you
declare an instance field. In fact, it's generally preferable to
including the initialization in a constructor, so long as the
initialization doesn't require more complex code than can be written in
a single expression, and it's universal throughout the class.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top