Why Cannot Access A Public Method From Another Class
Feb 12, 2015
Why can't I access a method from another class? For example, I want to get the value of get method from another class. It's giving me an error on if(getExamType() == 'M') That's what I've done, for example:
Java Code:
public static Exam[] collateExams(Exam[] exams){
Exam [] r = new Exam[50];
r = exams;
Exam [] finalExam = new Exam[50];
for(int i = 0; i < r.length; i++) {
if(getExamType() == 'M') {
}
}
return r; mh_sh_highlight_all('java');
I've got a CDI bean which is a facade for a JPA entity. Such entity has got a many to one relationship with itself and I've got the following method:
public Set<Account> getChildren() { return this.children; }
which is called in a JSF/Facelets page:
<h:commandLink action="#{accountController.destroy}" value="#{bundle.ListAccountDestroyLink}" rendered="#{item.children.size() == 0}"/>I then decided to return an unmodifiable set and changed getChildren() accordingly: public Set<Account> getChildren() { return Collections.unmodifiableSet(children); }
The page now reports this error:
java.lang.IllegalAccessException: Class javax.el.BeanELResolver can not access a member of class java.util.Collections$UnmodifiableCollection with modifiers "public" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65) at java.lang.reflect.Method.invoke(Method.java:588)
[Code] ....
javax.el.BeanELResolver is incorrectly failing because it's trying to invoke the size() method using reflection without correctly taking into account the method visibility (which is indeed invokable programmatically). I'm running NetBeans 6.8, Glassfish 3.0 and jdk1.6.0_17.
I have 2 classes. TestClassA has 1 getter and 1 setter. From my second class, TestClassB, I want to access the getter method of TestClassA. If I try to access it, I get null. How do I do it?I do not want the methods to be declared as static. How can the getter method value be printed in TestClassB (without marking anything as static)?
public class TestA { private String name; public String getName() { return name;
This time I have to make a Black Jack game ( I guess this is a classic) I have created Three classes for this BlackJack( Main), Card, and Player.
What I am trying to do is put the Give one card to the player and remove it from the deck into a separate procedure because I will be doing this several times during the game.
This is the code I have so far Under the class BlackJack.
I was doing coding exercise from a book ('OCP Java SE 6 - Practice Exams' by Kathy Sierra and Bert Bates). I came to a question that told to demonstrate the difference between 'default' and 'protected' access rules by creating/making a directory structure and putting a couple of classes in different packages.
For this, I made a total of four classes, out of which, three classes are-Car, TestingCars, CarDimensions. (The fourth is not yet used in testing code till now, so, I am giving only the other three classes.) Their coding is given below.
Out of these classes, the classes- TestingCars and Car - are in a directory (say, FolderName). And, the class- CarDimensions is in FolderName's sub-folder.
The class 'CarDimensions' is public (and its components too are public). And, I am testing all the classes from the class- 'TestingCars'. But, this class (TestingCars) is not able to find the public class- 'CarDimensions' which is in its sub-folder and gives two 'Cannot find symbol' errors citing the class-CarDimensions. Also, If all three classes are put in one single directory, the programs work, without any error.
Coding: Class TestingCars:class TestingCars { public static void main(String[] args) { Car c = new Car(); c.setType("FourWheeler");
[Code]....
I could not find why the public class- CarDimensions- is not getting found by the TestingCars class.
The compiler won't let me declare more than one class as "public". Am i correct in understanding that this is a java restriction ? This means i need to create a new file, for each public class that i want in a package ? The rest of the classes without access modifier will all be package-private. (Q has been asked before probably, but my search could not be narrowed).
I have a question about the following snippet concerning the steps the javac compiler follows to compile a program:
[...]at first, searching a class within a package is discussed if the latter doesn't contain a full package name[...]
It is a compile-time error if more than one class is found. (Classes must be unique, so the order of the import statements doesn't matter.)
The compiler goes one step further. It looks at the source files to see if the source is newer than the class file. If so, the source file is recompiled automatically. Recall that you can import only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compiler can easily locate source files for public classes.
However, you can import nonpublic classes from the current package. These classes may be defined in source files with different names. If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class. I don't quite understand the red fragment. I wondered if the word "import" nonpublic classes from the current package weren't a synonym for the word "use", since why would we want to import a class from the same package when compiler searches the current package automatically anyway?
However I wanted to test nonpublic classes that are contained in source file which name doesn't match the class name:
NonpublicClass.java:
Java Code:
package com.work.company; class NonpublicClass { public void description() { System.out.println("Working!"); } } mh_sh_highlight_all('java');
[Code] ....
Everything's fine when the source file names are the same as above. However, when I change NonpublicClass.java to a different name, there's an error "cannot find symbol" in:
Java Code: NonpublicClass v = new NonpublicClass(); mh_sh_highlight_all('java');
I noticed that the class file for NonpublicClass isn't even generated so that's probably the cause. If I change to the directory of the package the NonpublicClass is contained in and compile it directly, i.e. issue for example:
If i have a class(lets say class name is Approval) with the following private members: String recip_id, Int accStat, String pDesc, String startDate How can i create public get and setter methods for these private members of the class?
I am trying to make a program where a ball moves up continuously when you press space, then begins to move down when you reach a certain point. My method for this is to have a timer, and have a method that does this: When you press space, add 10 y coords every second (using a timer), and if you reach 470 y, then begin to drop 10 y coords. I made a method to hold the keylistener, and am running that method inside the actionPerformed method which is within another class. However, since it is a method, I cannot add my keylistener to my frame in the main method.
main error line 9 Java Code: import javax.swing.*; public class Main {
Why is it that a protected method cannot be accessed polymorphically outside its package. I suspect the reason is that some compile-time checks have to be performed. Is this correct?
I will like to add to the questions about constructors and its this. I have a class A with a constructor, i have a class B which initialize the constructor. also i have a class C which needs a variable in class A but doesn't need to initialize the constructor of A. the question how do i access the variable of class A without initializing the constructor.
now, i want to access a managed bean's method to execute a service call related to the code embedded in the hyperlink.
My Managed bean
@ManagedBean(name="details") @SessionScoped public class XXXX extends Bean implements Serializable{ public XXXX(){...... } public myMethod(..){ service.getDataRelatedToHyperlinkCode(....passing code here to fetch details from DB) } }
if i use postConstruct annotation it is getting executed only once since it is a session scope. and point to be noted is i cannot use viewscope and requestscope.
I've been unable to figure out how to access an objects data from another class. I ended up missing a lesson in java and haven't been able to catch up on this topic on my own through my textbook.
Error: has private access
Code:
public class TestCoffeeDrinker { public static void main(String[] args) { Coffee latte = new Coffee("Starbucks Tall Latte", 2.85); Coffee mocha = new Coffee("Starbucks Grande Mocha", 3.95); Coffee mcdonalds = new Coffee("McDonalds McCafe", 0.99); System.out.println(mcdonalds.toString());
why I cant access a variable from another class, in particular why in the main class cant i assign " quiz1 = getQuizOne();" ? It constantly giving me error.
Java Code:
import java.util.Scanner; public class Grade { private int quiz1, quiz2, midtermExam, finalExam = 0; public static void main(String[] args) { Student John = new Student(); John.StudentData();
I make a coding a subject system for my internship project, I use JComboBox to choose name of faculty. I want to call selected item of faculty from another class to choose subject under the selected faculty.
How can I call selected item from another class?
This is my coding for select faculty.
public class lectInterface(){ private void saveDataActionPerformed(java.awt.event.ActionEvent evt) { String staff_id=staffID.getText(); int staff_Id=Integer.parseInt(staff_id); String Name=staffName.getText();
[Code] ....
and below is coding for another class
try{ LectInterface lI = new LectInterface(); String host = "jdbc:derby://localhost:1527/STUDLECDB"; String uName = "studlecdb"; String uPass = "studlecdb"; Connection con = DriverManager.getConnection( host, uName, uPass );
I have a linked list of objects of a class I created called BaseballPlayer. I also have a Fielder and Pitcher class that extends BaseballPlayer. The linked list and list nodes accept elements of type BaseballPlayer. I put into the linked list both Fielder objects and Pitcher objects. The Pitcher class has a method that returns a float variable that is only found in the Pitcher class called EarnedRunAverage. However, in my main program it won't let me access the method. It says: The method getERavg() is undefined for the type BaseballPlayer. It does however, let me access methods in the super-class BaseballPlayer.
Here is main code and the Pitcher class code.
ListNode p = list.getFirst().next; while(p!=null){ if(p.player instanceof Fielder){ sortedFielders.append(Integer.toString(p.player.getNum()) +',');
import java.util.Arrays; import java.util.Comparator; class SimpleHolder extends Object { private final int value; public SimpleHolder(int value) { this.value = value;
[Code] ....
According to The Java Tutorial, static nested classes should not have access to other members of the enclosing class. I'd suppose to get compile-time error in the BasicComparator class. However, my code compiles just fine. Am I missing something?
I have two classes built for this assignment. One with getters, setters and constructor and a test class. The test class works fine up until the point when the user wants to (M)odify an employee that he/she built with the loadEmployee method. What happens is I need to enter multiple "M" inputs before anything pops up, and when something pops up, it's the displayMenu instead of modifyEmployee method. I imagine it has something to do with the amount of sc.nextLine()
import java.util.Scanner; // utilize scanner via console import java.text.NumberFormat; // allows numbers to be format in form of currency //Example of "big loop" in main to repeat using a No Trip (0,N) test first public class EmployeeTest { // space provided to make code easier on eyes...