|
| 10 Jul 2016 07:12 PM |
If so: In the most easy to understand way possible, can you explain the difference between static and nonstatic variables? |
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 07:19 PM |
| It means only 1 exists, that is when you create a new class the static variables are not created. It's great for constants and for other variables you only need 1 of across all classes. |
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 07:20 PM |
I don't think it is. :l
But if I had to guess, static variables won't allow people to change it. I don't do Java so..
If money grew on trees... It would be as valuable as leaves. | Twitter: @TwisterRBLX
|
|
|
| Report Abuse |
|
|
| |
|
|
| 10 Jul 2016 10:35 PM |
package com.jtcode
class Bump {
public static void main(String[] args){
System.out.println("Bump."); } } |
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 10:41 PM |
cntkillme already answered correctly. Static classes can't be created or initialized. Non-static classes can be created.
Sort of like on Roblox, Instance.new("Part") creates a new "Part" Instance. Static classes don't do that. Static classes are more like Tables in Lua.
|
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 10:47 PM |
"In the most easy to understand way possible, can you explain the difference between static and nonstatic variables?"
Static means that you access it by referring to the class - it is effectively 'permanent' in a sense that no derivative class "owns" it...
This is so that I can do this:
public class Animal { private static numOfAnimals = 0; private String name; public Animal() { this("animal"); }
public Animal(String name) { ++numOfAnimals this.name = name; }
public static getNumOfAnimals() { return numOfAnimals; } }
Therefore, when we derive a class like this:
public class Wolf extends Animal { public Wolf() //No args constructor just to differentiate between Wolf and Animal. { super("a Wolf"); }
public Wolf(String name) { super(name); }
public static void main(String[] args) { System.out.println(Animal.getNumOfAnimals()); //This will change depending on how many are made. //Refer to it statically by referencing it's class. } }
Static variables are tied to classes, not objects. If that helps any. |
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 10:51 PM |
| So static variables enable you to access it in an class(Wolf) that inherits Animal? |
|
|
| Report Abuse |
|
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 10 Jul 2016 10:53 PM |
I'm assuming its already answered but yolo
Static variables are class variables, you access them from the class itself Nonstatic are instance variables, like properties of the object and etc, you create a new one every time you create a new instance |
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 10:56 PM |
No, they are tied to the Animal class.
When Wolf does something to a static variable in Animal, it affects Animal's static variable, not 'Wolf's', because Wolf does not inherit Animal's static variables.
This is so that I can call Animal's static variable, even after creating Wolf's, and any derivatives of Animal (in this example) are able to add to that counter 'numOfAnimals', without individually inheriting them. (Wolf doesn't have a 'numOfAnimals' field. Animal does.)
What the static modifier does is allow something to be independant of polymorphism and object-oriented structure, and behave a bit more like C and C-like non-OOP languages do. (Except not really, but it's a better analogy than before.)
|
|
|
| Report Abuse |
|
|
|
| 10 Jul 2016 10:57 PM |
This allows me to do something else as well:
public class Car { public void printOutNumOfAnimalsToWatchOutFor() { System.out.println(Animal.getNumOfAnimals()); //Doesn't have to be attached at all. } }
|
|
|
| Report Abuse |
|
|