I am learning C++ and I learned that you can create your own types. I decided to try this in java. I am wondering if there is an easier way to make a bunch of variables private other then typing out private for each of the created variables.

for example in c++

#include <iostream>
#include <string>

using namespace std;

class monster{
private:
	int health;
	int mp;

public:
	monster (){
		health = 100;
		mp = 100;
	}

	int gethealth (){
		
		return health;
	}
	int getMP (){
		return mp;
	}


};

int main (){
	monster a;

	cout << "Health: " << a.gethealth() << endl;
	cout << "MP: " << a.getMP() << endl;

	char p;
	cin >> p;
	return 0;
}

This code creates a monster called a and then assigns 100 as the health and 100 as the mp and then prints it out. The health and the mp cannot be accessed directly because they are private, therefore I use the getHealth() method and the getMP() method to get the variables. I am wondering if there is a way to make the variables in java be private, by mentioning that they are private once and then that making them all private unless I mention otherwise like:

private:   // makes it private
	int health; // still private
	int mp;  // still private

public: // now its public
	monster (){  // still public
		health = 100; // still public
		mp = 100; // still public
	}
};

Thanks for any help.

nope, if you don't explicitly declare 'm private, they will have package visibility and accessability, which is, as I assume I don't need to explain, quite a different matter.

if you want your variables to be private, you'll have to type the 'private' access modifier for each of them.

Load the clipboard, position the cursor, right click and paste.

Ok thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.