[longish] Re: help errors[newbie]  
Author Message
Oscar kind





PostPosted: 2004-10-19 3:02:00 Top

java-programmer, [longish] Re: help errors[newbie] Madhur Ahuja <email***@***.com> wrote:
> Below is a simple program, which produces couple of errors.
> E:\programs\java1\concept\inner1.java:33: <identifier> expected
> t.somefunc(tt);
> ^
> E:\programs\java1\concept\inner1.java:33: package t does not exist
> t.somefunc(tt);
> ^
> 2 errors
>
> I cant seem to find the reason for these errors. Can anyone help
> me out.

Sure. Beware though, that I have a lot of remarks; also about coding
style and readability. It's a long read, but worth it (IMNSHO).

>
> public class inner1

- This name is confusing: it is an outer class with "inner" in the name...
- Please start class names with an upper case letter as in the JDK; when
most Java programmers read a name starting with a lowercase letter, they
expect a variable.

> {
>
> public static void main(String args[])
> {
> new test1();
> }
> }

This main method does nothing, unless you do everything in the
constructor. That is, your program runs when you create an object. This is
confusing.

>
>
> class test
> {
>
> private int a;
> test(int n)
> {
> a=n;
>
> }

This indentation is hard to read; you may use a bit more: 4 spaces or so.
If you run into the right margin, your code is most likely too complex.

Note that indentation, brace style, etc. is very personal, and has caused
many "holy wars" in the past. However, if it's readable, it's good.
Otherwise, it isn't. Opinions differ on what's readable though...

>
> void somefunc(test a)
> {
> a.a++;
> System.out.println(a.a);
> }
>
> }
>
> class test1
> {
> test t=new test(3);
> test tt=new test(4);
> t.somefunc(tt);
>
> }

This is the heart of the problem:
- t and tt are member variables, as they are defined outside any code
block.
- t.somefunc(tt) is a method call, but is outside a method, constructor or
even an initializer block. The compiler is expecting a definition for a
member variable, constructor, method or initializer.


You could make it work in several ways. Your goal is to understand why
option 2 is preferable.

Option 1:
- Double the braces in the definition of class test1. This creates an
initializer block that makes the lines inbetween into an anonymous
constructor that is always called when instantiating this class.

Option 2:
- Change the content of inner1#main(String[]) to:
test1 testObject = new test1();
testObject.callMethod();
- Perform the change of option 1.
- Between the two opening braces of the class test1, add this:
public void callMethod()
- Reformat the code (especially the class test1).


Regardless of which solution you choose, I advise you to read up on Sun's
coding conventions. Note they're just guidelines: you don't have to agree
with them. However, it helps if you follow them.
The URI is: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

Following them won't make you a better programmer, but it will help both
you and others to understand your code more easily. This in turn enables
you to get help more easily, and thus learn more.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2