Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By awinston
  • 1 Post By brocksoffice

Thread: Ok I have a few questions.

  1. #1
    brocksoffice is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Ok I have a few questions.

    I've been Reading Core Java Programming by Cay S. Horstman and Gary Cornell. I have to say that the chapter on Object Oriented Programming is somewhat vague.

    1.) My first question is why do you need to construct an object? I've seen code be run directly from a class, so why do you need to instantiate an object.

    2.) My second question is where is an object instantiated? Does it get instantiated inside a class or does it get instantiated outside of the class.

    3.) My third question is can you show me how the basic structure of an object being instantiated from a class looks like? With all the code needed to compile and run the example.

    4.) My fourth question is what does a constructor look like versus a method. Assuming the model looks like the following
    {
    constructor
    constructor
    ...
    method
    method
    ...
    instance field
    instance field
    ...

    I know that you might tell me to just do a google search, but I've tried googling these questions and I didn't come up with answers. In my book there are many snippets of code, but not very many full examples that the developer can create.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Ok I have a few questions.

    Thankfully, Oracle has anticipated some of these questions:

    1. What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    2. An object is instantiated inside a class. You can instantiate the object when you define a variable:

    Java Code:
    String word = "hello";
    ...or you can define a variable and instantiate its object later inside a method:

    Java Code:
    String word;
    ...
    public static void main(String[] args)
    {
        word = "hello";
    }
    3.Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects) (There is a workable demo that you can download).

    4. What a constructor looks like: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) What a standard method looks like: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Last edited by awinston; 07-28-2012 at 07:02 PM. Reason: clarification
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Ok I have a few questions.

    Quote Originally Posted by awinston View Post
    ...or you can define a variable and instantiate its object later inside a method:

    Java Code:
    String word;
    ...
    public static void main(String[] args)
    {
        word = "hello";
    }
    ...
    Please be careful when using instance variables inside of static methods. Perhaps you wanted

    Java Code:
    static String word;
    ...
    public static void main(String[] args)
    {
        word = "hello";
    }
    awinston likes this.

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Ok I have a few questions.

    Quote Originally Posted by Fubarable View Post
    Please be careful when using instance variables inside of static methods. Perhaps you wanted

    Java Code:
    static String word;
    ...
    public static void main(String[] args)
    {
        word = "hello";
    }
    That was a bad example on my part. Let me fix that:

    Java Code:
    String word;
    ...
    public void someMethod()
    {
        word = "hello";
    }
    Let me clarify that the above is legal although more confusing and not a better choice than this:

    Java Code:
    String word = "hello";
    Fubarable likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Ok I have a few questions.

    brocksoffice, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    brocksoffice is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Ok I have a few questions.

    DarrylBurke my mistake... sorry I'll be more descriptive in my next title post.
    DarrylBurke likes this.

Similar Threads

  1. 3 questions
    By silverglade in forum New To Java
    Replies: 11
    Last Post: 05-09-2011, 03:17 PM
  2. I have 2 questions =P
    By santa in forum New To Java
    Replies: 4
    Last Post: 01-19-2011, 05:35 PM
  3. Questions
    By Csharks in forum New To Java
    Replies: 8
    Last Post: 01-19-2011, 04:11 AM
  4. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM
  5. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •