Access Private Member
May 7, 2014
public class StudentNumber {
/*
public StudentNumber(){
System.out.println("test");
}
*/
private char c='W';
public StudentNumber(float i){
System.out.println(i);
[Code] ....
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - c has private access in extention.pkgsuper.StudentNumber
at extention.pkgsuper.ExtentionSuper.main
I did cast type. So what's the problem?
View Replies
ADVERTISEMENT
Feb 9, 2015
Okay, I am supposed to implement the functionalities of the Set class using a private data member of type ListReferencedBased<E>,how the ListReferenceBased works with what I am trying to accomplish.I am trying to complete Set.java, and I have barely started and much of the code doesn't work. ListReferenceBased was given to me completed.
import java.util.Iterator;
pubic class ListReferenceBased<E> implements ListInterface<E>, Iterable<E>{
/** reference to the first element of the list */
private Node<E> head;
/** number of items in list */
private int numItems;
[code]....
View Replies
View Related
May 23, 2015
Java Code:
public class MountainBike extends Bicycle {
// the MountainBike subclass has
// one field
public int seatHeight;
// the MountainBike subclass has
// one constructor
public MountainBike(int startHeight, int startCadence,
[Code] ....
At first,
Java Code: public int seatHeight; mh_sh_highlight_all('java');
tells us that seatHeight is NOT a static field (because of the absence of static keyword).
Whereas in the constructor, the absence of dot notation (like something like this.seatHeight) in
Java Code: seatHeight = newValue; mh_sh_highlight_all('java');
shows that it IS a non-member/static variable.
How does this make sense?
View Replies
View Related
Jul 9, 2014
I think the following code should trigger a compiler error.
public final class IPoint3D {
private double x;
private double y;
private double z;
public IPoint3D(double x, double y, double z)
[Code] .....
View Replies
View Related
Feb 18, 2014
Class UserAssessBean{
private String username;
private int userid;
private ArrayList<ModuleBean> module;
--{get/set}--
[Code] ....
How can i access the getters/setters of module bean, when it was returned as array list in UserAssessBean?
View Replies
View Related
Nov 5, 2014
i have two classes in two different files.i have this class:
Java Code:
public class Color
{
private int red;
private int green;
private int blue;
public Color(){
red = 0;
green = 0;
blue = 0;
} mh_sh_highlight_all('java');
And i have this class :
Java Code:
public class Light
{
private Color color1;
private boolean switchedon;
public Light(int red, int green, int blue){
//dont know what to write here . how can i use the members of the Color class here ? without using extends.
}
} mh_sh_highlight_all('java');
View Replies
View Related
Jul 10, 2014
I can understand basic concepts such as OOP, Threads, Events and GUI, I've coded a little but I've always had this bothering me:
Explaining by example:
public class X{
int x;
String y;
public static void main(String[] args){
x = 10;
y = "hello ";
[Code] ....
Okay, i'm pretty certain that code won't work, but I just want to show you conceptually, not actually care whether the code works or not. In case I wanted to get that code to work I should have probably used a Swing application to get a KeyListener in the first place, but I guess I know that, and if this was working, what would happen theoretically is, the code would run, initialize x to 10, and y to hello, then when a key is pressed, it will update x to 11 and y to hello world, I'm pretty sure that's what happens.
In this case, I used a inner class to update it's parent's members, I've seen this done before and I can vouch for the fact that it's a legitimate way to code a class.
Now in this example:
class X{
int x;
String y;
public int getX(){ return x; }
public String getY(){ return y; }
[Code] ....
So in this overly complicated example, i'm trying to share class X with both class Y and class Z, just that how to share the members of class X with the different classes without making a new instance of X. In the previous example, I could access the parent's members because the inner class was implicitly capable of accessing the parent's members. However in this case, If "Y" starts an instance of X, then how do I access it? because it's a side by side class not a hierarchy for me to access the parent's members.
View Replies
View Related
Mar 9, 2014
I have a Tcr object as a member variable of the JFrame. But When ChangeListener swings into action, the variable inside it are all nulls. the TcrPanel is created before the ChangeListener is triggered.
Tcr tcrPanel;
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JTabbedPane) {
CloseButtonTabbedPane pane = (CloseButtonTabbedPane) e.getSource();
[Code] ....
View Replies
View Related
Feb 24, 2014
I've tried a couple ways to do it, and they don't work. I'm aiming for functionality like I got with the regular for loop, but from an enhanced for loop. Is this simply beyond the scope of an enhanced for loop, or am I just not getting the right syntax?
TestObject to1 = new TestObject("first", 11);
TestObject to2 = new TestObject("second", 12);
TestObject to3 = new TestObject("third", 13);
TestObject to4 = new TestObject("fourth", 14);
TestObject to5 = new TestObject();
List<TestObject> testList;
testList = new ArrayList<TestObject>();
[code]....
The TestObject class is simply an int and a String, with getters getInt and getString. It all works fine with the regular for loop.
edit: I should probably mention that I know what I have in the enhanced for loop now will only display the class name and the hash. I've tried adding the .getString and .getInt, and tried a few other ways to make it work. I just reverted to this because it compiles and runs
View Replies
View Related
Apr 22, 2014
Got a problem with generics, which I'm still pretty new at. Here's a program that compiles fine:
import java.util.ArrayList;
import javax.swing.JComponent;
public class Experiments {
public static void main(String[] args) {
ListHolder holder = new ListHolder();
[Code] ....
It's useless, but it compiles. If I change Line 14, however, to add a generic type parameter to the ListHolder class, Line 10 no longer compiles:
import java.util.ArrayList;
import javax.swing.JComponent;
public class Experiments {
public static void main(String[] args) {
ListHolder holder = new ListHolder();
[Code] ....
I get this error:
Uncompilable source code - incompatible types: java.lang.Object cannot be converted to javax.swing.JComponent
at experiments.Experiments.main(Experiments.java:10)
Apparently, the introduction of the type parameter leaves the compiler thinking that aList is of type Object. I can cast it, like this:
JComponent c = ((ArrayList<JComponent>)holder.aList).iterator().next();
That makes the compiler happy, but why is it necessary? How does adding the (unused) type parameter to the ListHolder class end up making the compiler think the aList member of an instance of ListHolder is of type Object?
View Replies
View Related
Oct 27, 2014
The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.
I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.
Java Code: public void myFunction () {
int [] myInt; // A local, member variable (because "static" keyword is not there) declared
} mh_sh_highlight_all('java');
So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?
View Replies
View Related
Mar 19, 2015
So I'd like to know what all the statement, not sure if it's the correct way of calling them but I'd like to know what like all of those purple-ish colored words in eclipse actually mean.Here's what I know so far, so if you can add some stuff to it or just correct me:
public - can be accessed by different classes.
private - can't be accessed by different classes.
static - adds a '.' which pretty much lets you like use methods on it? Not really sure about this one.
final - a final value of a variable meaning it couldn't and willn't change?
super - I have no clue, maybe something that has to be executed first? Not sure.
void - bassicly you don't have to use return as it doesn't return any value.
this - Uh-.. I think it has something to do with the class this keyword has been entered into, not quite sure what it does though.
I assume there are alot more but I am just not sure about these common ones, what the actually do and what's their purpose?
View Replies
View Related
Mar 25, 2014
What happened when we use main() as a private. . . . .???????
View Replies
View Related
Jan 22, 2015
I am creating a slot machine using eclipse. I am trying to get the "winnings" JTextField to be updated in a way so that when the random images have been selected it adds to the number that is already displayed in the JTextField as opposed to what it is doing at the minute which is just displaying how much was won on that particular spin. I am also struggling to set a code for when noting is won, nothing is added. My code is below.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
[Code] ....
View Replies
View Related
Apr 29, 2014
It isn't messing up it just keeps making me make my arraylist static and i'm not sure why.
public class driver
{
private static ArrayList<AddItems> items;
public driver()
[Code]....
View Replies
View Related
Sep 22, 2014
I'm working on a project that contains multiple classes. Each class contains and must contain only PRIVATE variables. Here's my issue. When my test code calls for a new instance of "StudentClass" as so:
StudentClass studentClass = new StudentClass(offeredClass.getClassIdNumber(),
offeredClass.getClassName(),
offeredClass.getClassroom());
The corresponding constructor won't let me initialize it's variables because they are declared private within another class, as shown here:
StudentClass (float classIdNumber, String className, Classroom room){
this.classIdNumber = classIdNumber;
this.className = className;
this.room = room;
}
When getClassName, getClassroom, and getClassIdNumber are passed to a toString() method elsewhere in my test code. the output is returned just fine. When passed through the StudentClass, I'm getting Null across the board.
View Replies
View Related
Jul 6, 2014
The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
[code]....
View Replies
View Related
Jan 16, 2015
I have a project for a class where I'm supposed to be something with private arrays and private integers and I still don't understand the point of private anything within java. if I want to change a variable I'll change it. if I want it to stay the same I'll leave it the same. so what point is all this private/public nonsense unless I'm trying to stop hackers or something?
View Replies
View Related
Jan 30, 2015
Java Code:
import java.util.Scanner;
import java.util.ArrayList;
public class Problem1
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
[Code] ....
There is an error and says that my ArrayList has private access. I can't figure out how to fix it.
The code runs but when I enter "Quit", the program just stops. The arraylist isn't printed out?
View Replies
View Related
Jan 7, 2014
When creating a class with a constructor, why does one have to create private variables (attributes) to be used as parameters by the object? The object's parameters will be set to be exactly equal to the private variables (attributes), so what is the point of having the private variables (attributes) Why are both private variables (attributes) and parameters needed when they are set to be equal each other anyway?
View Replies
View Related
Apr 9, 2015
I just recently started learning about encapsulation, how to set variables private and only accessible to other classes through setters() and getters(). I've seen a couple of programming examples, perhaps not enough but some from here, and I sort of built the assumption that by default all variables need to be private. to make things more clear, here's a card dealer I made which simply
1- generates a fulll deck of 52 cards
2- lets user decide how many players, with 5 as Max number allowed as each player is dealt 10 cards.
3- deal cards
I approached this by making A deck , card , player and game class
import java.util.ArrayList;
public class Deck {
//an Object of this Class would generate a full deck ie an ArrayList of 52 Cards
private String[] suits={"Spades","Diamond","Clubs","Hearts"};
private int[] number={1,2,3,4,5,6,7,8,9,10,11,12,13};
ArrayList<Cards> deck= new ArrayList<Cards>();
[code]....
I can understand why for example Deck class's suit and number arrays are set to private , as they only need to be accessed by this class only. however, both the Deck class's deck arraylist and the Player class arraylist are not private, as I need to transfer these values from one to the other and it felt to me that setting them to private would make it more difficult to deal with them, but what I did instead is to set the Game class dealCard(), which is the only method that have access to them as private. does this achieve the same effect or do I need to set both of these arrayList to private?a follow up question, this is more related to the actual card dealer program, in this code
private void dealCards(){
for(int x = 0 ; x < playerCount ; x++){
for(int y = 0 ; y < 10; y++){
playerList.get(x).pile.add(deck.deck.get(0));
deck.deck.remove(0);
}
}
}
is there an API in ArrayList class that moves(adds to receiver and remove from giver) element across ArrayLists?
View Replies
View Related
May 14, 2015
I came across the below
1) When a variables are declared "Private" How should it be accessed from the driver class ? Sometimes i get an error in driver class saying "your variable is declared Private" why am I getting this error ...
The document says "Private" declared variables should be accessed only through methods. What does that mean.
View Replies
View Related
Sep 10, 2014
As below code showing that you cannot directly access the private variable as i understood,
public class EncapsulationDemo{ private int ssn;
private String empName; private int empAge; //Getter and Setter methods
public int getEmpSSN(){ return ssn; }
public String getEmpName(){ return empName;
[Code] .....
View Replies
View Related
May 2, 2015
I have a class named Base and a private variable named _hopcount i have 10 instances of class base i use _hopcount as creteria to some if but other instances edit _hopcount so i want to prevent _hopcount edit by other instances; I want to have private variable which other instances of same class can't modify it.
public class Base extends TypedAtomicActor {
private int _hopcount = 0;
if(_hopcount <= 3) {
some code;
}
public function() {
_hopCount += 1;
}
}
View Replies
View Related
Feb 12, 2015
I want to make a private method that will change the value of a instance variable like this:
String bla = "bla bla bla";
private void changeString (String x) { x = "eee";}
changeString(bla);
Now value of the variable bla should be "eee" but its not, it is stil "bla bla bla".
View Replies
View Related
Dec 10, 2014
I have problems with private static void print section, something is missing? And in case 4, I want it to stop the program(end the loop) but it keeps going.
import java.util.Scanner;
import java.util.ArrayList;
public class Dogregister16 {
public static ArrayList<Dog> dogregister = new ArrayList();
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
initiate();
[Code] ....
case 4:
System.exit(0);
System.out.println("Exit program");
}
}
}
}
View Replies
View Related