I have the following java class:
class Outer
{
private Integer a;
private Long b;
class Inner
{
...
|
If I want to create a form that adds people to a List, how do I have access to that List from another class? Where would I define that List ... |
my question is im getting d jtable but i want to display the data from the database into the the jtable.
if i pass data directly its being displayed in the jtable ... |
I am having a small problem I am trying to print the contents of a couple of variables which are located in a a private method. but I simply keep getting ... |
package com.valami;
public class Ferrari
{
private int v = 0;
private void alam()
{
System.out.println("alam");
}
public Ferrari()
{
System.out.println(v);
...
|
interface Test {
public void test();
}
public class TestMain {
private String h = "AAA";
public static void main(String[] args) {
TestMain t = new TestMain();
}
public TestMain() {
Test ...
|
i hope that i mean my words.
i have a class like this:
public class MainClass extends JFrame{
private JLabel mainlabel;
private SampleClass sample=new SampleCalss();
...
|
|
The point of a private variable is that you can only get it by calling a method. This means that the class is free to change the way it stores the information, without hurting other classes. Imagine we have a class: class T1 { public String url; } and another class uses it like: T1 myt1; myt1.url = "http://www.javaranch.com/"; System.out.print(myt1.url); We ... |
I have got a bean CraneAsset, which extends Asset. "Asset" has a property called "assetNum" and appropriate get/set methods. When I create a CraneAsset bean and use getAssetNum, I got a message that "assetNum" is a private variable in the superclass "Asset". So I went back to the "Asset" and changed "assetNum" to "protected". But making it "protected" to get "assetNum" ... |
Ok lets say I have a class called AltTreeMap. In that class one of the class variables is: private Node root. Now in my node class I want to access the root in this fashion: if (root==null).. how do I do this? If I try using this.root the compiler thinks "this" refers to a Node, not to the AltTreeMap class. If ... |
indeed. There is a strong school of thought that says you shouldn't generally have setters at all, just getters, and set every field through the constructor only (essentially making as many objects immutable as possible). You could say you're providing the option of increased security when using setters as you can write them to provide for example validation (or even authorisation), ... |
|
Can someone explain to me the logic behind making varaiables private (so that other classes CAN NOT access or change the variables) and then adding accessor methods (so that other classes CAN access or change the variables)??? I don't get it? There has to be a reason because it is in every JAVA book I am reviewing. But, it appears inherently ... |
This is a question about programming style. Everyone knows that encapsulating your variables is a Good Thing (TM). Obviously one class wants to access another class's private variables, it must do so via getter/setter methods. My question is this: When a class wants to access its own private variables, should it do so via getter/setter methods or directly? On the one ... |
public class Obj { private int val = 0; public int getVal() { return val; } public void changVal(Obj o) { o.val = 3; } public static void main(String[] args) { Obj o1 = new Obj(); Obj o2 = new Obj(); o1.changVal(o2); System.out.println("o1:" + o1.val + " o2:" + o2.val); } } |
This question is either really naive or just not possible Here is what ive typed into Jcreator Pro: //VARIABLE TO HOLD Candidate CHOICE private int choice = 0; count1; count2; count3; count4; count5; count6; count7; maxVote; My question is, when having multiple variables am I to use ";" just to separate them or after the private int choice = 0 am ... |
Generally, if you are within the same class, the convention is to access the instance variable directly. An exception to this might be if the getter did some sort of error checking, and you wanted that error checking to occur each time you use the instance variable; then that work could be centralized in the getter, and the getter would be ... |
Hello, I am trying to compare two objects by overriding equals(Object o) method. Code: public class TestEquals{ private int a; private int c; private boolean val=false; public boolean equals(Object o){ TestEquals te=null; if(o instanceof TestEquals) { te = (TestEquals)o; if((this.a==te.a)&&(this.c==te.c)) val=true; else val= false; } return val; } How can I access private variables a&c using te.a and te.c?? I thought ... |
|
Hi All, I want to access a varible form my super class. But i am having problem to access that. Below is my code : public class A { Private int price=0; } Public class B extends A { public int getPrice() { return price; } } If i declare varible as 'Private' then i am not able to access it ... |
Hi, Please have a look at the code below. public class Person { private String name; public Person(String name) { this.name = name; } public static void main (String[] args){ new Person("Java").equals(new Person("Java")); } public boolean equals(Object o) { if ( ! (o instanceof Person) ) return false; Person p = (Person) o; p.name = "andy"; return p.name.equals(this.name); } } This ... |
Hello all, I'm a little bit confused about this: I know that private istance variables are non heritaged but public method yes. So if I had two classes like this: public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Employee extends Person { private ... |
Without reflection it's not possible from outside the outer most class. Private members of a nested class are visible in its enclosing class. For example: class Test { static class Test2 { private static final int i = 13; } public static void main(String[] args) throws Exception { System.out.println(Test.Test2.i); // compiles since Test2 is nested within Test } } class Test3 ... |
|
Okay, so I keep coming across the practice of making class variable private and using getter and setter methods for other classes to interact with these variables. I tried looking for some explanations but what I have found is that it is "good practice" to do so. However, as I code, sometimes I find my code is longer and it is ... |
|
Hi^^, thank you for the article you have supplied. Two lines used in the article are interesting.They are as follows: 1.Information hiding serves as an effective criterion for dividing any piece of equipment, software or hardware, into modules of functionality. 2.When a computer program is well designed decomposing the source code solution into modules using the principle of information hiding, evolutionary ... |
How to access private variable of a class in other class ? the deeper meaning of private members (=variables) is that they are NOT accessible from another class. if you want to do that, you have to use Introspection to temporarily change from private to public, do your access, and then restore private state. Thats the way spring does such things. ... |
I looked at your hello and Goodbye test class and did guess "hello". I am pretty sure that with a few tricks I will hopefully be able to keep some of the variables private access. My original example is pretty average so for that i apologise. Its hard to explain the problem im having since the program is a work in ... |
|
Hi I am developing an application which simulates a University. I have to read in data from a file, the data which i am having problems with is a private class variable which keeps track of the number of lecturer objects. I am reading the data in from a different class from the one which stores the class variable. Whats the ... |
|
|
Saish is a very helpful person. I remember one time when I asked his advice about how I should tackle a difficult problem. "Should I program to interfaces? Should I use RMI-IIOP over Banyan Vines with LU 6.2? What about NetBeans? Who played Whitey on 'Leave It To Beaver?' I just don't know what to do, Saish!" and he said, " ... |
|
|
|
What is the use in declaring a variable as private . Because if the user has to modify the variable we have to provide the user with get and set methods . He will declare an Object of that class and calls set /get methods. Instead we could have declared that variable as public and allow him to change directly?Is it ... |
|
Well, the syntax isn't correct so that won't even compile. And no, it's not inefficient. You don't have a clue about the real bottlenecks in applications (network, database, etc.) if you're worried about having classes as member variables. Or did you plan on using only primitive values for your programs? Then you're using the wrong language, you want C. |
azjp wrote: Now. the following contradicts my perception that private instance methods can be accessed from within any instance method of an object of the same class. No it doesn't. It just a) invalidates your assumption that calling a method via reflection is identical to calling it normally, and/or b) shows that you didn't read the docs for getMethod(), getMethods() closely ... |