Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help, Object Oriented / Class & Object.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Need help, Object Oriented / Class & Object.

    Hello,

    I need your help once again, so far, i have this following codes and im trying to make a java program that calls an object value initialized.

    public class TestClass
    {
        public String name;
        public int age;
     
        public void myMessage()
    	{
    	System.out.println("Hello I'm" + name + "and i'm" + age + "years old");
    }
    }

    what i want is to put the following codes of the object.. so once i compile and run the project it executes the value of an object from the method i defined above.

    TestClass myName = new TestClass();
    myName.name = "Jefferson";
    myName.age = 18;

    my question is, it is possible to declare or define it in the same project? or i need to do this separately?

    could anyone pls help me? thanks in advance


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Need help, Object Oriented / Class & Object.

    Hi. I'm not sure if I understand what you mean by "in the same project", but the second part of the code, i.e.,

    TestClass myName = new TestClass();
    myName.name = "Jefferson";
    myName.age = 18;

    can certainly be placed within the TestClass class itself, in the body of the main method. See Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started) if you're unsure about the main method.

  3. The Following User Says Thank You to jashburn For This Useful Post:

    jedmustdie (February 23rd, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help, Object Oriented / Class & Object.

    Quote Originally Posted by jashburn View Post
    Hi. I'm not sure if I understand what you mean by "in the same project", but the second part of the code, i.e.,

    TestClass myName = new TestClass();
    myName.name = "Jefferson";
    myName.age = 18;

    can certainly be placed within the TestClass class itself, in the body of the main method. See Lesson: A Closer Look at the "Hello World!" Application (The Java™ Tutorials > Getting Started) if you're unsure about the main method.
    Thank you for your answer, i've almost done doing this exersice and i'd separately make a new class which contained the main() method for the execution of the program and add the method i used from TestClass project to call it and print the initialized value of the object at the main class, now time to move about the contructor.

Similar Threads

  1. Object Oriented Programming
    By cruzer66 in forum Object Oriented Programming
    Replies: 0
    Last Post: November 20th, 2013, 02:14 PM
  2. Class & object
    By perves in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 11th, 2013, 10:14 PM
  3. read & write object in object file
    By amirsarem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2013, 11:35 AM
  4. Object oriented programming
    By merr78 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2012, 12:32 AM
  5. Object-oriented applet
    By mjpam in forum Object Oriented Programming
    Replies: 26
    Last Post: September 15th, 2010, 06:43 AM