Hey guys. I'm really confused about instance and class and static methods, variables, and how they can or cannot access each other. Anyone have some simple examples for a noob?:c-penguin:
14 replies to this topic
#1
Posted 13 March 2011 - 09:50 PM
|
|
|
#2
Posted 14 March 2011 - 01:24 AM
An instance method is a method that is associated with the instance of a particular class, namely the methods that can accept using the this reference. A "static" method and a "class" method are more-or-less the same thing, as they are methods for the entire class itself. The same goes for variables, and the simple way to think of access is that instances clearly have access to static class values (methods and objects), whereas static methods do NOT have access to a particular instance unless a reference to an instance is passed. Consider the following source code:
public class Main
{
// This static int is accessible from any Main object or derivative:
protected static int count;
// This int is accessible by any Main instance, but it's associated to a particular Main instance.
private int instanceVal;
static
{
// This is a "Static initializer block", which you can think of as a constructor for the Class
// object rather than a constructor for a particular instance of a Class.
count = 0;
}
public Main(int val)
{
++count; // Access the static int instance.
instanceVal = val;
// Remember that this assignment implicitly resolves to:
// this.instanceVal = val;
// and that "this" resolves to the particular instance of Main you're targeting.
}
// The main method happens to be a static method, so I saw no reason to write
// another one to explain static methods. This method does NOT need an instance
// of Main to run, so you could very simply just call Main.main(null) instead of
// calling it on a particular instance.
public static void main(String[] args)
{
Main first = new Main(10);
Main second = new Main(20);
// A static method of a class may access static values in that class:
System.out.println("Current number of Main instances: " + count);
// But it may not access instance variables directly, it must choose
// an instance! The following won't compile:
// instanceVal = 10;
// Each instance of Main has a different value for instanceVal, take a
// look:
System.out.println("Values of instanceVal: " + first.instanceVal +
" " + second.instanceVal);
}
}
This is how Java treats static and instance methods/values. If there are any other questions don't hesitate to ask.
Wow I changed my sig!
#3
Posted 14 March 2011 - 05:24 AM
its a very useful post.
#4
Posted 14 March 2011 - 07:43 PM
I kinda understand it, I'll try to compile it in Eclipse and take a better look at it tomorrow morning. Looks like it has everything in their for me to know. Thanks a lot, really appreciate it :c-smile:.
#5
Posted 15 March 2011 - 01:41 PM
I read somewhere that you should keep all variables private. Why is that?
#6
Posted 15 March 2011 - 02:11 PM
It's just good programming style to keep private as many variables as possible. A class should offer functionalities through methods, hiding its internal mechanisms.
#7
Posted 15 March 2011 - 10:02 PM
eafkuor said:
It's just good programming style to keep private as many variables as possible. A class should offer functionalities through methods, hiding its internal mechanisms.
Is it also the same as saying that private variables are only available for methods within the class the variables are declared?
#8
Posted 15 March 2011 - 11:33 PM
They are available everywhere in the class. from the beginning '{' to the ending '}'
Unless the methods are static, they only have access to static fields.
Unless the methods are static, they only have access to static fields.
#9
Posted 17 March 2011 - 10:45 AM
Is it okay if I post all of my nooby questions here, because they're all pretty basic. I don't feel like making threads for all little questions. I feel embarrassed, lol.
My next question is, how do nested classes work? And why would someone want to nest classes? Any examples?
My next question is, how do nested classes work? And why would someone want to nest classes? Any examples?
#10
Posted 17 March 2011 - 10:47 AM
I think Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) gives an answer to most of your questions.
#11
Posted 17 March 2011 - 07:21 PM
That's confusing. Too much static, instance, inner, outer, enclosing classes which is really confusing me. :sad:
#12
Posted 18 March 2011 - 12:54 PM
Nested classes.
As you know you write a normal class like so:
Main reasons to use an inner class:
You can't make an object (create) an inner class without having an outer class object/instance.
To go on about accessing the private fields.
Why's that a reason?
Well if you got a class and you want to maximise encapsulation.
Imagine if it's a GUI class, with buttons, buttons need an ActionListener to be used.
If the button is pushed, and then it has to update a textField, textArea, labels.
You would need to provide getters/setters to access them outside the class, reducing the encapsulation.
Now other classes could also access the Gui class' button, textfield and label.
If that's undesired, you may consider putting the ButtonListener inside the Gui class.
And now no getters and setter must be provided=Maximising encapsulation.
As you know you write a normal class like so:
public class MyClass{
...
}
An inner class is, like the name suggests, inside the class.
public class MyClass{
...
class OtherClass{
...
}
}
Main reasons to use an inner class:
- Other classes can't reach / use it. Better encapsulation.
- The inner class can access all methods of the outer class (including private fields).
You can't make an object (create) an inner class without having an outer class object/instance.
To go on about accessing the private fields.
Why's that a reason?
Well if you got a class and you want to maximise encapsulation.
Imagine if it's a GUI class, with buttons, buttons need an ActionListener to be used.
If the button is pushed, and then it has to update a textField, textArea, labels.
You would need to provide getters/setters to access them outside the class, reducing the encapsulation.
public class Gui extends JFrame{
private JButton button;
private JTextField textField;
private JLabel label;
...
}
public class ButtonListener implements ActionListener{
private Gui gui
...
public void actionPerformed(ActionEvent e){
Gui.getButton().setText("klik");;
Gui.getTextField().setText("klik2");;
Gui.getLabel().setText("klik3"); ;
}
}
Now other classes could also access the Gui class' button, textfield and label.
If that's undesired, you may consider putting the ButtonListener inside the Gui class.
public class Gui extends JFrame{
private JButton button;
private JTextField textField;
private JLabel label;
...
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
button.setText("klik");
textField.setText("klik2");
label.setText("klik3");
}
}
}
And now no getters and setter must be provided=Maximising encapsulation.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









