Class in another file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S. McKee

    Class in another file

    Hi,

    I am very (very very) new to java and have what I am sure is a simple
    problem that I hope someone can help with. I have two .java files. The first
    has

    public final class FormatSWN {

    public FormatSWN() throws Exception {

    etc etc

    }

    etc etc
    }

    This file compiles without an errors. I am wanting to call the FormatSWN
    class from within another file. The code in that file is

    class CallSWN {
    public static void main(String[] cName) {

    FormatSWN();
    }
    }

    This file returns a compile error saying "Cannot resolve symbol" and has the
    ^ pointing at the start of FormatSWN();

    Both these files are in the same directory so that should not be an issue.

    I know this will be really simple, but it has me stumped. If someone has the
    answer, that would be great

    Steve






  • Ulf_N

    #2
    Re: Class in another file

    S. McKee skrev:[color=blue]
    > Hi,
    >
    > I am very (very very) new to java and have what I am sure is a simple
    > problem that I hope someone can help with. I have two .java files. The first
    > has
    >
    > public final class FormatSWN {
    >
    > public FormatSWN() throws Exception {
    >
    > etc etc
    >
    > }
    >
    > etc etc
    > }
    >
    > This file compiles without an errors. I am wanting to call the FormatSWN
    > class from within another file. The code in that file is
    >
    > class CallSWN {
    > public static void main(String[] cName) {
    >
    > FormatSWN();
    > }
    > }
    >
    > This file returns a compile error saying "Cannot resolve symbol" and has the
    > ^ pointing at the start of FormatSWN();
    >
    > Both these files are in the same directory so that should not be an issue.
    >
    > I know this will be really simple, but it has me stumped. If someone has the
    > answer, that would be great
    >
    > Steve
    >
    >
    >
    >
    >
    >[/color]

    FormatSWN is a class, it is the "blueprint" for an object of type
    FormatSWN. You have to create an *instance* (object) of the class before
    tying to call any of its methods. Like so:

    FormatSWN myFormatSWNObje ct = new FormatSWN();

    Now you can call methods using your new object:

    myFormatSWNObje ct.aMethodInFor matSWN();

    (The identifier 'myFormatSWNObj ect' is what you decide to call your new
    object. The method 'aMethodInForma tSWN' is a method (you) defined in
    class FormatSWN.)

    /ulf

    Comment

    • S. McKee

      #3
      Re: Class in another file

      Thanks !!

      I also managed to make it work by

      try {
      new FormatSWN(new String[]{cName[0]});
      } catch etc etc

      I have one more question if I may. In the FormatSWN class I have a method
      called Speakable, that has to use the Vector command to turn something like
      123 into one, two, three.

      The method is called by

      Vector speakable = Speakable(strin put); //strinput is from the call to
      FormatSWN (see original post below)

      The Speakable method has

      Vector Speakable (String[] strinput) {
      int i;
      int x = 0;
      String strword;
      Vector strspeakable = new Vector();

      for (i=0; i < strinput.length ; i++) {
      x = Integer.parseIn t(strinput[i])

      some switch commands on x & a strspeakable.ad d go here
      }

      My problem is that the first time in I would expect strinput.length = 3 and
      x to equal 1 (if 123 is passed), then 2, then 3 etc, but instead the
      strinput.length =1 and x = 123 ?

      Can you tell me what is wrong ?

      Thanks and sorry for asking obvious questions !

      Steve

      "Ulf_N" <ulf@dontlikesp am.se> wrote in message
      news:Mbb1e.1333 48$dP1.471210@n ewsc.telia.net. ..[color=blue]
      > S. McKee skrev:[color=green]
      > > Hi,
      > >
      > > I am very (very very) new to java and have what I am sure is a simple
      > > problem that I hope someone can help with. I have two .java files. The[/color][/color]
      first[color=blue][color=green]
      > > has
      > >
      > > public final class FormatSWN {
      > >
      > > public FormatSWN() throws Exception {
      > >
      > > etc etc
      > >
      > > }
      > >
      > > etc etc
      > > }
      > >
      > > This file compiles without an errors. I am wanting to call the FormatSWN
      > > class from within another file. The code in that file is
      > >
      > > class CallSWN {
      > > public static void main(String[] cName) {
      > >
      > > FormatSWN();
      > > }
      > > }
      > >
      > > This file returns a compile error saying "Cannot resolve symbol" and has[/color][/color]
      the[color=blue][color=green]
      > > ^ pointing at the start of FormatSWN();
      > >
      > > Both these files are in the same directory so that should not be an[/color][/color]
      issue.[color=blue][color=green]
      > >
      > > I know this will be really simple, but it has me stumped. If someone has[/color][/color]
      the[color=blue][color=green]
      > > answer, that would be great
      > >
      > > Steve
      > >
      > >
      > >
      > >
      > >
      > >[/color]
      >
      > FormatSWN is a class, it is the "blueprint" for an object of type
      > FormatSWN. You have to create an *instance* (object) of the class before
      > tying to call any of its methods. Like so:
      >
      > FormatSWN myFormatSWNObje ct = new FormatSWN();
      >
      > Now you can call methods using your new object:
      >
      > myFormatSWNObje ct.aMethodInFor matSWN();
      >
      > (The identifier 'myFormatSWNObj ect' is what you decide to call your new
      > object. The method 'aMethodInForma tSWN' is a method (you) defined in
      > class FormatSWN.)
      >
      > /ulf[/color]


      Comment

      • Ulf_N

        #4
        Re: Class in another file

        S. McKee skrev:
        <snip>[color=blue]
        >
        > Vector Speakable (String[] strinput) {
        > int i;
        > int x = 0;
        > String strword;
        > Vector strspeakable = new Vector();
        >
        > for (i=0; i < strinput.length ; i++) {
        > x = Integer.parseIn t(strinput[i])
        >
        > some switch commands on x & a strspeakable.ad d go here
        > }
        >
        > My problem is that the first time in I would expect strinput.length = 3 and
        > x to equal 1 (if 123 is passed), then 2, then 3 etc, but instead the
        > strinput.length =1 and x = 123 ?
        >[/color]

        Your result indicate that strinput only contains one string, in the
        first element, and that string is "123". It should contain *three*
        elements, holding the strings "1", "2", and "3", respectively. (I think
        that you perhaps should read a bit more about how classes, objects,
        arrays, etc. works in java. It's boring, I know, but it will save you
        lots of time. I promise.) /ulf

        Comment

        • Ted Dunning

          #5
          Re: Class in another file


          You should look at what the difference between the string "123" and the
          value returned from "123".split ("") is. The first is a string with
          three characters and the second is an array that contains four strings,
          "", "1", "2", "3". Contemplation on the difference should be
          illuminating.

          On style, I would recommend that you use a List instead of a Vector. A
          List is a data type from the Collections framework which provides a
          much better overall selection of classes and allows you to choose when
          to use thread synchronization .

          Another point is that you seem to be trying to do something useful in a
          constructor. If you aren't trying to create an object to use later,
          you might be better off using a static method. I should point out that
          using static methods is often an indication of poor design.

          Comment

          • Robert Maas, see http://tinyurl.com/uh3t

            #6
            Re: Class in another file

            > Newsgroups: comp.lang.java
            (Added comp.lang.java. programmer because comp.lang.java is not valid.)[color=blue]
            > From: "Ted Dunning" <ted.dunn...@gm ail.com>
            > I should point out that using static methods is often an indication
            > of poor design.[/color]

            I respectifully disagree with your generalization. If you are defining
            a new data structure, different from anything already available in the
            Java API, then it's appropriate to have one or more constructors to
            create such structures and one or more methods to provide access or
            mutation upon such structures. But if you are merely defining methods
            that work with already-existing data structures and/or primitive data
            types, that were defined in classes whose source you are not allowed to
            modify (and might not even be able to see), then it is impossible for
            you to make instance methods for processing those objects, so you have
            no choice but to use static methods, which are completely appropriate
            for such use.

            Note: You might still choose to sub-class the API class, not define any
            new data members if you don't need any, but do define additional
            instance methods that are available only for types of your sub-class.
            One problem with that tactic is that you can't directly apply your
            methods to objects of the base class, as you can for static methods.
            You can't even down-cast objects of the base class to apply your
            derived-class instance methods to them. You need to construct objects
            *originally* of your derived class, then you can apply methods of both
            base and derived classes. Or if you are given an object of the base
            class you need to pick apart all the data members and re-build an
            object of your derived class, a royal pain. Better to just use a static
            method which can be directly applied to base-class objects.

            What indicates poor design is using an inappropriate type of method,
            static method when instance method is more appropriate, or vice versa.

            Comment

            Working...