Results 1 to 4 of 4
  1. #1
    bradrg is offline Member
    Join Date
    Aug 2014
    Rep Power

    Default SOLVED- Creating objects - confused

    Hi Everyone. First post

    So I'm still trying to get to grips with Java, and like to understand exactly why I'm doing something, so that I am not just regurgitating the code, and have run into something that I don't understand and would appreciate some help

    If I want to create an object from class "Apples", I would use the following, right?

    Apples MyAppleObject = new Apples();

    From what I understand, MyAppleObject is the new object name, new -> creates a new instance of it in memory, and Apples() is the onCreate method that is called

    So question 1: (just a quick aside question) Can I create an object without calling Apples()?
    Apples MyAppleObject = new;

    Question 2: - PARTLY SOLVED - I discovered that (Button) is a way of typecasting, so I understand that line a little better. What I don't understand is why we don't need to initialize the object with "new"

    I've now looked at a bit of android development and xml and those declarations are all together different, and I'm not sure why. I haven't found a single explanation for the difference in format.

    Java Code:
            Button Add;
    	protected void onCreate(Bundle savedInstanceState) {
    		Add = (Button) findViewById(;
    So the Button object is declared above the onCreate method, but initialized afterwards I guess....
    But instead of using Button Add = new Button() they use Add = (Button) findViewById(;
    Why? Can someone break it down for me. What java principles are they using here? ie what is up with the () etc.

    Question 3:

    then In XML they use the following:

    Java Code:
    public*static*void*main(String[] args){
    ********// Creates a DOM object in memory. Now you can access
    ********// data in the xml file
    ********Document xmlDoc = getDocument("./src/tvshows5.xml");
    Once again, why didn't they have to use

    Document xmlDoc = new Document()

    Thanks in advance
    Last edited by bradrg; 08-18-2014 at 09:17 AM.

  2. #2 is offline Senior Member
    Join Date
    Mar 2011
    Rep Power

    Default Re: Creating objects - confused

    Question 1: The new operator invokes a constructor. So you cannot use new alone. Review this link: Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Question 2: In this line: findViewById( returns an existing object that you are assigning to the reference Add. However, you must first cast that object to a Button.

    Question 3: This line returns an existing Document object: getDocument("./src/tvshows5.xml")

  3. #3
    bradrg is offline Member
    Join Date
    Aug 2014
    Rep Power

    Default Re: Creating objects - confused

    An existing Object... that makes sense. Thanks.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Creating objects - confused

    "Apples() is the onCreate method "

    You're mixing up Android stuff (that is, the onCreate()) with straight forward Java (the Apples() constructor).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. creating objects in java
    By pp100 in forum New To Java
    Replies: 11
    Last Post: 04-05-2012, 08:28 AM
  2. classes and creating objects
    By Latanyar in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 07:14 PM
  3. creating dynamic objects?
    By alacn in forum New To Java
    Replies: 4
    Last Post: 06-04-2010, 03:51 AM
  4. Confused about date objects
    By Sharke in forum New To Java
    Replies: 7
    Last Post: 05-10-2010, 07:24 AM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 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