Jump to content

Question regarding nested class's

- - - - -

  • Please log in to reply
4 replies to this topic

#1
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
First off I would like to say I love the avatars :D

Recently, well around 2 weeks ago, I started learning java for an internship that I have received. Due to my experience in C++, C, Python and Ruby I have been able to pick up Java fairly quickly. I also have a heavy competitive gaming background, so I figured one of the best ways to help me learn about using a strictly class driven language would be to try to make a text based game.

I have just started it so I do not have a lot complete but I have run into a couple things that I would like cleared up.

First being Nested Class's:
If I have a Character class that for now consist of a Character name, a private int Char_health and 3 objects of a different class called level_class (Such a creative name I know ^^). For visual purposes I will post below.

public class Character {

	private String char_name = null;

	level_class char_attack = new level_class("Attack");

	level_class char_strength = new level_class("Strength");

	level_class char_defence = new level_class("Defence");

	private int char_health = 10;

	public Character(String Name){

		char_name = Name; 

        }

}

Level Class:
public class level_class {

	private String Skill;

	private int lvl;

	private int xp;

	private int xp_to_go;

	public level_class(String skill_name){

		Skill = skill_name;

		lvl = 1;

		xp = 0;

		get_xp_to_go(lvl);

	}

	public int get_lvl(){

		return lvl;

	}

	public int get_xp(){

		return xp;

	}

	public int get_xptogo(){

		return xp_to_go;

	}

	void xp_gained( int xp_gain){

		if ( lvl == 50){

			xp += xp_gain;

		}

		else {

			if ( xp_gain < xp_to_go){

				xp += xp_gain;

				xp_to_go -= xp_gain;

			}

			else if (xp_gain > xp_to_go){

				int temp = xp_gain - xp_to_go;

				xp += xp_to_go;

				lvl += 1;

				System.out.println("Congrats you just reached level" + lvl + " in your" + Skill +" skill");

				get_xp_to_go(lvl);

				xp_gained(temp);

			}

			else {

				xp += xp_to_go;

				lvl += 1;

				System.out.println("Congrats you just reached level" + lvl + " in your" + Skill +" skill");

			}

		}

	}

	

	private void get_xp_to_go( int level){

		switch(level){

		case 1: xp_to_go = 83; break;

		case 2: xp_to_go = 91; break;

		case 3: xp_to_go = 102; break;

		case 4: xp_to_go = 112; break;

		case 5: xp_to_go = 124; break;

		case 6: xp_to_go = 138; break;

		case 7: xp_to_go = 151; break;

		case 8: xp_to_go = 168; break;

		case 9: xp_to_go = 185; break;

		case 10: xp_to_go = 204; break;

		case 11: xp_to_go = 226; break;

		case 12: xp_to_go = 249; break;

		case 13: xp_to_go = 274; break;

		case 14: xp_to_go = 304; break;

		case 15: xp_to_go = 335; break;

		case 16: xp_to_go = 369; break;

		case 17: xp_to_go = 408; break;

		case 18: xp_to_go = 450; break;

		case 19: xp_to_go = 497; break;

		case 20: xp_to_go = 548; break;

		case 21: xp_to_go = 606; break;

		case 22: xp_to_go = 667; break;

		case 23: xp_to_go = 737; break;

		case 24: xp_to_go = 814; break;

		case 25: xp_to_go = 898; break;

		case 26: xp_to_go = 990; break;

		case 27: xp_to_go = 1094; break;

		case 28: xp_to_go = 1207; break;

		case 29: xp_to_go = 1332; break;

		case 30: xp_to_go = 1470; break;

		case 31: xp_to_go = 1623; break;

		case 32: xp_to_go = 1791; break;

		case 33: xp_to_go = 1977; break;

		case 34: xp_to_go = 2182; break;

		case 35: xp_to_go = 2409; break;

		case 36: xp_to_go = 2658; break;

		case 37: xp_to_go = 2935; break;

		case 38: xp_to_go = 3240; break;

		case 39: xp_to_go = 3576; break;

		case 40: xp_to_go = 3947; break;

		case 41: xp_to_go = 4358; break;

		case 42: xp_to_go = 4810; break;

		case 43: xp_to_go = 5310; break;

		case 44: xp_to_go = 5863; break;

		case 45: xp_to_go = 6471; break;

		case 46: xp_to_go = 7144; break;

		case 47: xp_to_go = 7887; break;

		case 48: xp_to_go = 8707; break;

		case 49: xp_to_go = 9612; break;

		case 50: xp_to_go = 0; break;

		}

	}

}


Right now its all very basic and will most likely change as I continue to work on it.

Since I can not see any other class aside from Character using level_class should I nest it? Does that make it easier to access certain values?
On top of that if I want to access variables within an object in an object do I have to make get functions in each class?
Like so...
	public void get_skill_stats( level_class char_attack){

		System.out.println("Your" + char_strength.get_Skill() + " lvl is " + char_strength.get_lvl());

		System.out.println("Your" + char_strength.get_Skill() + " xp is " + char_strength.get_xp());

		System.out.println("Your" + char_strength.get_Skill() + " xp_to_go is " + char_strength.get_xptogo());	

	}


or is there a much easier way of accessing them

Summary:
1: What is the benefit from nesting a class versus having it in a different file in the same package.
2: What is the easiest way/syntax to grab this values if lets say I was calling the Character class from a different file called Main.

Btw, as I said I am still learning Java so if I am doing something a wrong way and there is a much easier/clean one then please let me know, Thanks!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

1: What is the benefit from nesting a class versus having it in a different file in the same package.
It basically puts restrictions on the nested class.
  • Other classes apart from the "surrounding" class can't use it, (meaning there's no "new NestedClass();")
  • The nested class can only exist if the surrounding class exists, and NOT the other way around.
  • The nested class can access the surrounding class's attributes like if it were its own attributes, regardless of being private.
Those restrictions are, certainly for Java, a good thing as we like to encapsulate as much as possible and expose as less as possible.

Quote

2: What is the easiest way/syntax to grab this values if lets say I was calling the Character class from a different file called Main.
2 options:
  • Like you said create a getter for level_class, and then get the attribute.
  • Write a method in character which delegates it onward to its level_class.
    Like if you want to get the xp, you write this in the character class.
    public[B] [COLOR="#000080"]int[/COLOR][/B] getAttackXp(){
    
      [B][COLOR="#000080"]return[/COLOR][/B] char_attack.get_xp();
    
    }

Quote

Btw, as I said I am still learning Java so if I am doing something a wrong way and there is a much easier/clean one then please let me know, Thanks!
Naming conventions:
So your classes (level_class) should start with a capital letter.
I'm not sure if there's a real naming convetion about it, but camelCase is used a LOT, and underscores rarely (never saw it actually :P).


You don't use an access level modifier at the level_class declarations:
private String[B] char_name = null;

>>>level_class[B] char_attack = [B][COLOR="#000080"]new[/COLOR][/B] level_class([B][COLOR="#008000"]"Attack"[/COLOR][/B]);

....
This isn't wrong. But know that not using an access modifier makes the variable accessable be all other classes in the same package.
Java says to put them as private as possible. It can be correct if other classes in the package use it, but consider making em private and using getters.
more info: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)


public Character(String Name){

	char_name = Name; 

}
Variables (Name) must not start with a capital letter (naming convention again). (Same with Skill in level_class)
In constructors and setters it's common to use the same name as the variable, to let Java know which one you need you can use "this" to point at the class variable and not at the local variable.
Example:
public Character(String char_name ){

	[B][COLOR="#000080"]this[/COLOR][/B].char_name =char_name ; 

}


#3
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
Thank you so much, all that info really did help a lot.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

wim DC said:

Other classes apart from the "surrounding" class can't use it, (meaning there's no "new NestedClass();")
This isn't true. Consider this example:
import somepack.Other;


public class Test

{

    public static void main(String[] args)

    {

        Other clz = new Other();

        Other.NestedClass onc = clz.getNestedClass();

        System.out.println(onc.value);

    }

}
package somepack;


public class Other

{

    public class NestedClass

    {

        public NestedClass()

        {

            value = 10;

        }


        public int value; // It's okay, this is just a test program.

    }


    public Other()

    {

        the_class = new NestedClass();

    }


    public NestedClass getNestedClass()

    {

        return the_class;

    }


    private NestedClass the_class;

}
This will function correctly. So long as the class is public, any other class may access it. To show it's not just package access, I separated their packages.

wim DC said:

The nested class can only exist if the surrounding class exists, and NOT the other way around.
This is true, but only to the extent that you don't use the static keyword. If you do, then the nested class may be created independently of an existing external class reference. Modify the classes slightly like so and test again:
import somepack.Other;


public class Test

{

    public static void main(String[] args)

    {

        Other.NestedClass onc = new Other.NestedClass();

        System.out.println(onc.value);

    }

}
package somepack;


public class Other

{

    public static class NestedClass

    {

        public NestedClass()

        {

            value = 10;

        }


        public int value; // It's okay, this is just a test program.

    }

}

wim DC said:

The nested class can access the surrounding class's attributes like if it were its own attributes, regardless of being private.
This is, again, also true, and is one of the big advantages of nested classes (You can think of nested classes, in many ways, replacing the functionality of friend classes in C++). But it cannot access any non-static members if the class is static, so that matters as well.

wim DC already went through most of the naming convention stuff, and I agree that level_class isn't that good of a name for a class (It's expected that class names are UpperCamelCase in Java), but I frequently use the lowercase_underscores naming convention for variables. I use lowerCamelCase for method names, and most people stick to using lowerCamelCase for variable names as well so I don't particularly mind it, I just have a preference to lowercase_underscores.
Wow I changed my sig!

#5
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
The underscores in naming convention is from c like languages with no caps at all what so ever :) and would be used as a norm there.

For java and other high level languages, camel case seemed to be the way to go about.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users