![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Just a minor nitpick. 'protected' in general OO design means that a class member is visible/accessable to derived classes (sub-classes). I believe package-wide visibility is the 'default' specifier in Java which is given when the private/protected/public keyword is omitted from the name of the class member entirely. Since packages are a Java-specific thing, there's no general OO term to describe it |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (08-01-2007) | ||
| |||
| public class dust { int latin=200; public static void main(String[] args) { int sand=0; if(args.length==1) { sand=Interger.parseInt(args[0]); } else { //when i am trying to access my latin variable over here i am getting n error //why is it so sand=latin; } } } Last edited by nogono : 08-07-2007 at 12:13 PM. |
| ||||
| Code: public class dust
{
int latin=200;
public static void main(String[] args)
{
int sand=0;
if(args.length==1)
{
sand=Interger.parseInt(args[0]);
}
else
{
//when i am trying to access my latin variable over here i am getting n error
//why is it so
sand=latin;
}
}
} Code: public class dust
{
private static int latin=200; // I edit this one
public static void main(String[] args)
{
int sand=0;
if(args.length==1)
{
sand=Interger.parseInt(args[0]);
}
else
{
//when i am trying to access my latin variable over here i am getting n error
//why is it so
sand=latin;
}
}
} |
| |||
| public class dust { private static int latin=200; // I edit this one public static void main(String[] args) { int sand=0; if(args.length==1) { sand=Interger.parseInt(args[0]); } else { //when i am trying to access my latin variable over here i am getting n error //why is it so sand=latin; } } } But i didn't get why int latin=200 should be static...... |
| |||
| Code: public class Dust {
private static int latin_static=200;
private int latin_instance=100;
public static void main(String[] args) {
Dust dust1 = new Dust();
Dust dust2 = new Dust();
dust2.latin_instance++; //dust2.latin_instance = dust2.latin_instance + 1
dust2.latin_static++; //dust2.latin_static = dust2.latin_static + 1
System.out.println("\t\tdust1\tdust2");
System.out.println("static\t\t" + dust1.latin_static + "\t" + dust2.latin_static);
System.out.println("instance:\t" + dust1.latin_instance + "\t" + dust2.latin_instance);
}
} You need to make your latin variable static because main is a static method. The alternative is to create an instance of the Dust class and then use it: sand=new Dust().latin;. Why can't you use instance variables within a static method? Well for one thing, which instance of the class would the instance variable be taken from? There are no instances of the Dust class in your original code! If you are up for some reading, you can check out Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) for more information. __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (08-09-2007) | ||
| |||
| An access modifier used in a method, field or inner class declaration. It signifies that the member can only be accessed by elements residing in its class, subclasses, or classes in the same package. //this is the definition of protected consider two classes in same package in first class i declare variable as protected //warning both classes are independent...there is no subclass if i now make an object of first class in second class still i am able to access variable(protected) of first class.....how come... if yes how come...if no how come |
| ||||
| Quote:
|
| |||
| Quote:
a static variable exists exactly once in the program, for the entire duration of that program - its created before main() runs, and destroyed after main() finishes. Furthermore, that variable belongs to the class inside which it (the variable) is declared On the other hand, a "non"-static variable exists as part of an object. When an object is created by new, the variable is created as a part of that object - and that variable is destroyed whenever the object is destroyed. - If another new object is created, then again, that "non"-static variable is created, again, as a component of the new object. Essentially, the variable belongs to the obejct. In the context of your problem, you were attempting to access a non-static variable which didn't exist, because there was no object attached for it to exist with. making the variable static ensured that the variable did exist when you needed it inside main() if you had done something like this, you could have omitted the static keyword from your earlier program Code: public class dust //class is called dust
{
int latin; //latin is "non"-static
public static void main(String args[])
{
dust my_dust = new dust(); //make a new dust class object in 'my_dust'
my_dust.latin = 5; //OK, latin belongs to the object inside 'my_dust'
}
} Last edited by Bench : 08-19-2007 at 04:17 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |