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 4 of 4

Thread: Help with making classes as a variable type!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with making classes as a variable type!

    Hi, I'm quite new to java, and I've come to the point where there are just some things that I don't get no matter where I look or what I read up.

    I've been confused with using a created class as a variable type for quite some time now. Take this class for example:

    public class Dragon {
     
    	private String name;
    	private boolean breathesFire;
     
    	public Dragon(String name, boolean breathesFire) {
    		this.name = name;
    		this.breathesFire = breathesFire;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	public boolean isBreathesFire() {
    		return breathesFire;
    	}
     
    	public void setBreathesFire(boolean breathesFire) {
    		this.breathesFire = breathesFire;
    	}
    }

    This was provided by my lecturer. In another class, he has done this:

    package com1003.objectville.composition;
     
    public class Knight {
     
    	private String name;
    	[B]private Dragon dragon;[/B]
     
    	public Knight(String name) {
    		this.name = name;
    	}
     
    	public Knight(String name, Dragon dragon) {
    		this.name = name;
    		this.dragon = dragon;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	public Dragon getDragon() {
    		return dragon;
    	}
     
    	public void setDragon(Dragon dragon) {
    		this.dragon = dragon;
    	}
     
    	public String toString() {
    		String s = "Knight: "+name;
    		if (dragon != null) {
    			s += ", his dragon is: "+dragon.getName();
    		}
    		return s;
    	}
    }

    Now I've made my area of concern in bold above. You see how he makes the private variable "dragon" type "Dragon"? So he wrote "private Dragon dragon" instead of "private int dragon" or something that I usually see. And this type "Dragon" is the class name of the first piece of code provided. I don't see how you can do this, and why you do this.

    Could someone explain what's going on, and why it's done this way in the code above?

    Thanks a lot, I really appreciate it.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Help with making classes as a variable type!

    he wrote "private Dragon dragon" instead of "private int dragon" or something that I usually see. And this type "Dragon" is the class name of the first piece of code provided. I don't see how you can do this,
    Well, you do it as your teacher has illustrated. There is a dragon associated with each knight. In code this is expressed by putting a field declared as Dragon in the class Knight.

    and why you do this.
    You do this because not everything can (conveniently) be expressed as an int. The knight might have an age, for instance, and this could be expressed using an int field:

    public class Knight {
     
        private String name;
        private int age;
        private Dragon dragon;

    But there is only so much you can do with an int. A knight might make it one larger each year (and eat cake), but that's about it.

    The knight might also want to address his dragon: "<insert name here>, prepare to do battle!". But ints don't have names, so this rather natural sort of behaviour would be impossible if the dragon variable were just an int. But, by declaring dragon as of type Dragon the programmer can use the fact that it has a getName() method and write:

    System.out.println(dragon.getName() + ", prepare to do battle!");

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help with making classes as a variable type!

    Quote Originally Posted by Kranti1992 View Post
    Now I've made my area of concern in bold above. You see how he makes the private variable "dragon" type "Dragon"? So he wrote "private Dragon dragon" instead of "private int dragon" or something that I usually see. And this type "Dragon" is the class name of the first piece of code provided. I don't see how you can do this, and why you do this.

    Could someone explain what's going on, and why it's done this way in the code above?

    Thanks a lot, I really appreciate it.
    Hello Kranti1992!
    You will face many cases in your OO programming life that beside primitive type variables you will need to use object references. An object is an instance of a class created by you with a set of attributes and methods which perform certain tasks.
    I would definitely suggest you reading the java tutorial for Objects.

    EDIT: For a better explanation read pbrockway2's answer.
    Last edited by andreas90; April 20th, 2012 at 06:44 PM.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with making classes as a variable type!

    According to me,
    i think You are passing object of the Dragon class in Knight constructor, you need variable of type Dragon class to hold that object.
    otherwise you can't hold value.
    int Dragon can't be taken--- as Knight(_,Dragon dragon), here dragon contains two variables that are declared in first code..
    so using this type of variable you can access the variable of Dragon in knight class.
    Hope you find this helpful for understanding

Similar Threads

  1. Making a Java quiz with Classes
    By diagnonsense in forum Java Theory & Questions
    Replies: 2
    Last Post: March 26th, 2012, 06:53 AM
  2. Making A Quiz With Loops, Incompatible Type Error
    By diagnonsense in forum Java Theory & Questions
    Replies: 3
    Last Post: February 17th, 2012, 04:50 PM
  3. can Java create a user define type of classes?
    By LearningJava in forum Java Theory & Questions
    Replies: 5
    Last Post: September 30th, 2011, 10:49 AM
  4. Variable Type-Checking
    By richip in forum Collections and Generics
    Replies: 7
    Last Post: March 17th, 2011, 06:25 PM
  5. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM

Tags for this Thread