Need helpl from a java expert

Discussion in 'OT Technology' started by Pudge, Jan 17, 2009.

  1. Pudge

    Pudge New Member

    Joined:
    Nov 24, 2008
    Messages:
    2,572
    Likes Received:
    0
    Location:
    Athens, GA
    Now I know I will get a lot of shit answers, but I am hoping at least ONE of you could help me:

    I created a file called "ClassA.java" with only one public void method called "greeting" that simply prints out a message. I was also asked to create a file called "Test.java" with a main method that prints out a message. That same file also needs to create a new instance of 'ClassA' as the reference variable 'instanceA'. Test.java also has to call the 'greeting()' method of 'instanceA'.

    I need help with the task in bold.

    *on a side note, both are in a folder "src". In order for one to reference the other do I need to type 'package src;' in both files?
     
  2. CodeX

    CodeX Guest

    Well since you already assumed a lot of our answers will be "shit" answers I am too afraid to answer you for the possibility of you deeming it a "shit" answer.
     
  3. Pudge

    Pudge New Member

    Joined:
    Nov 24, 2008
    Messages:
    2,572
    Likes Received:
    0
    Location:
    Athens, GA
    No I meant ppl just coming in a posting random unnecessary or COMPLETELY offtopic answers. Any post that anyone thinks could help would be much appreciated.
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    ClassA instanceA = new ClassA();

    Thats calls the constructor of ClassA and creates your new object.
     
  5. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Something like this? I don't use Java much anymore so hopefully you can fix any basic mistakes I might have made.

    ClassA.java:

    Code:
    public class ClassA
    {
        public void greeting()
        {
            System.out.println("Hello!");
        }
    }
    Test.java:

    Code:
    public class Test
    {
        public static void main(String[] args)
        {
            ClassA instanceA = new ClassA();
            instanceA.greeting();
        }
    }
     
  6. Pudge

    Pudge New Member

    Joined:
    Nov 24, 2008
    Messages:
    2,572
    Likes Received:
    0
    Location:
    Athens, GA
    Thank you so much! Been a month or so and I forgot how to use constructors and call classes :big grin:
     
    Last edited: Jan 18, 2009
  7. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    Yeh, juust include a constructor in his code and you are good to go.
     
  8. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    +1
     

Share This Page