Creating New Non-static Inner Class Outside Of Parent Class
Nov 22, 2014
I have an Abstract Class called GameColorEffect which contains a number of non-static Inner Classes that extend their Parent Class, GameColorEffect. I want to be able to create instances of the Inner Classes, however my IDE, eclipse, prompts me with the error:
No enclosing instance of type GameColorEffect is accessible. Must qualify the allocation with an enclosing instance of type GameColorEffect
And eclipse shows me a possible solution which is to turn the Inner Classes to static, this would allow me to create instances, but not really. This is because using methods from the static Inner Classes that change values in the Inner Classes will do this for every instance of the same Inner Class which is literally like a single instance. However, I want these Inner Classes to be individual with their values and still be able to use them outside as instances. I've found out a possible solution, which I'm not sure works like I want it to:
Java Code : GameColorEffect = new GameColorEffect.ExampleEffect(); mh_sh_highlight_all('java');
However, this is in-compact because sometimes all I need is to use just a method like:
Java Code : new GameColorEffect.ExampleEffect(intensity).applyEffect() mh_sh_highlight_all('java');
And another solution that I already knew prior was that I could make the Inner Classes proper classes not inside of the GameColorEffect class, but this is also in-compact because I will have to have so many classes for the so many effects that I have.
View Replies
ADVERTISEMENT
Feb 7, 2014
I've a parent class with a argument constructor like below(a sample code)
public class Parent {
Parent(String name) {
System.out.println(name);
}
public static void main(String[] args) {
}
}
Also I've child.class which extends Parent.class as shown below,
public class child extends Parent {
child(String name) {
super(name);
}
}
Now, I want create/modify the constructor which is in child, by taking "int i" as an input instead of "String name". How can I do that? Run time I want to execute child constructor not a parent constructor.
Condition is: Without making any changes to the Parent class
View Replies
View Related
Mar 1, 2015
Does child class gets a copy of the methods and variables of parent class?
public class test1 {
public static void main(String a[]) {
Child c = new Child();
c.print();
[Code] ....
why is the output 1?
View Replies
View Related
May 28, 2014
Regarding the lifecycle of servlet , in headfirst servlet i can find :
You normally will NOT override the service() method, so the one from HttpServlet will run. The service() method figures out which HTTP method (GET, POST, etc.) is in the request, and invokes the matching doGet() or doPost() method. The doGet() and doPost() inside HttpServlet don’t do anything, so you have to override one or both. This thread dies (or is put back in a Container-managed pool) when service() completes.
How can I call the doGet method of the subclass from the superclass. i am not getting this .
View Replies
View Related
Apr 14, 2015
I have a quick polymorphism question. I have a parent class and a sub class that extends the parent class. I then declare an array of parent class but instantiate an index to the sub class using polymorphism. Do I have to have all the same methods in the child class that I do in the parent class? Here is an example of what I mean.
public class ParentClass
{
public ParentClass(....){ }
public String doSomething(){ }
}
public class ChildClass extends ParentClass
{
public ChildClass(....)
[Code] ....
Is polymorphism similar to interfaces where the child class needs all the same methods?
View Replies
View Related
Feb 4, 2015
I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator
class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");
[code]....
View Replies
View Related
Jun 11, 2014
I am wondering if there is a way in jave to use enums WITHIN a class (without creating a separate enum class) without using private static final. Something like as folows:
class My Class {
myEnum {ACTIVE, INACTIVE, PENDING};
}
is there something like this available?
View Replies
View Related
Jul 3, 2014
I am working on a project involving a class that has the attributes of one of its inner classes. Now, if possible, I would like to make it so that the inner class is not visible outside of the class. Also, some of the functional mechanics require that the class be an instance of the nested inner class (it extends the inner class). The following code snippet demonstrates the situation.
public class A extends A.B {
public static class B { //ideally I would like this to be private/protected.
}
}
When I try to compile this program, I get the error message "Cyclical inheritance involving A." This error does not make much sense because, since the inner class "B" is static, it requires no instance of "A" (it does not inherit from "A" or uses it). My question is "Is it possible to do something similar to this structure?" I have searched many forums in search of the answer but have not found anything that attempts to explain it. The closest problem that I have found is one relating to the inheritance of a nested inner class from another class. I would like to express that the problem that I am having involves a class defined within the inheriting class.
View Replies
View Related
Mar 10, 2014
class Test3 {
} class MySub extends Test3 {
}
class Test4{
public static void main(String args[]) {
MySub m = new MySub();
}
}
I learned that if a class and its parent class both have no constructors, the compiler is supposed to complain. When I compiled Test4, i got no errors. why did it give no errors?
I read this article : [URL] ....
View Replies
View Related
Jun 10, 2014
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. There is also a MyDate class as explained below. A person has a name, address, phone number, and email address. A student has a status (freshman, sophomore, junior, or senior). Define the status as an integer which can have the value 0 (for "Freshman"),
1 (for "Sophomore"),
2 (for "Junior"), and
3 (for "Senior"),
but don't allow the status to be set to any other values. An employee has an office, salary, and dateHired. The dateHired is a MyDate field, which contains the fields: year, month, and day. The MyDate class does not explicitly inherit from any class, and it should have a no-arg constructor that sets the year, month, and day to the current year, month, and day. The MyDate class should also have a three-argument constructor that gets three int arguments for the year, month and day to set the year, month and day.
A faculty member has office hours and a rank. Define the rank as a String (for values like "Professor" or "Instructor"). A staff member has a title, which is also a String. Use data types for the fields as specified, or where one is not specified, use a data type that is appropriate for the particular field. Write a test program called TestEveryone.java that creates a Person, Student, Employee, Faculty, and Staff object, and invoke their toString() method (you don't need to call the objects' toString() method explicitly).
Note: Your MyDate.java class is the object class that your dateHired field is created from in the Employee.java class.
Do not use the Person, Employee or Faculty classes defined on pages 383 and 384 of the book. Create new ones.Here is the code I have so far concerning the employee and MyDate.
public class Employee extends Person {
private String office;
private double salary;
//private MyDate dateHired;
//7 argument constructor for employee
public Employee(String name, String phoneNumber, String email, String address, String office, double salary /*MyDate dateHired*/) {
super(name, phoneNumber, email, address);
[code]....
View Replies
View Related
May 20, 2015
In the process of creating a new class, I need to move my main method from the class SaveDate to the class DynamicTest. Below I have listed the code of both classes.The objective is to be able to run my program from the DynamicTest Class. I need understanding the process of moving my main method to a different class and creating an Object of a class and calling its method.
public class SaveData {
private static final Map<String, Object> myCachedTreeMap = new TreeMap<String, Object>();
public static final List<String> getLines(final String resourceParam, final Charset charset) throws IOException{
System.out.println("Please get: "+resourceParam);
if (myCachedTreeMap.containsKey(resourceParam) ) {
// Use the cached file, to prevent an additional read.
[Code] ......
View Replies
View Related
Jun 27, 2014
From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?
View Replies
View Related
May 26, 2015
Alright, I have two classes, this one
public class Player {
private String player;
public String getPlayer() {
return player;
}
private int strength;
private int defense;
[Code] .....
However, it says that under Player.getPlayer() that it 'Cannot make a static reference to the non-static method'.
View Replies
View Related
Jan 29, 2014
I'm working on a program using GUI. My problem is that I'm reaching **1000 Line** and this is stressful because all my code are in one file. I'm not liking this. So I research the internet about Inheritance. However, what I know about Inheritance that Inherit everything in the parent class. However, when I try to use a variables in the child class to override it in the parent class I can't for example let say that I have in the parent class something like this:
JButton getButton = new JButton("Enter");
Now when I go to the child class. I want to Inherit and use this variable so I can use the ActionListener on the getButton and override for the parent class, but it's not working.
This is my code:
public class H_Store extends JFrame {
private String s ="Kinds: ";
private JButton calculateButton;
private JPanel mainPane;
private JPanel getProfitPanel;
private JTextField ground_M_QTextField;
[Code] ....
What I want to do exactly is to take the last code into another class or do something with it so I can use it in the Parent class, in other word any math calculation method or class I want them outside the Parent class. I mean this code :
private class CalcButtonListener implements ActionListener{
// vairbles for the ground Meat check box
private double total_GM;
private double weightPrice_1;
private String stringQ;
private String stringW;
private String stringP;
[Code] ....
View Replies
View Related
Oct 28, 2014
Why I can create an Instance of a class that contains non static variables within the static main method ?
This program runs fine
import java.util.*;
public class Test{
public int j;
public static void main(String[] args) {
Test test1=new Test();
System.out.println(test1.j);
[Code] .....
View Replies
View Related
Nov 18, 2014
This is a someway special question, because I am using jmonkeyEngine.
But the topic is simple:
I have 2 classes:
public class Spielbrett extends SimpleApplication {
public static void main(String[] args) {
Spielbrett app = new Spielbrett();
app.start();
}
@Override
public void simpleInitApp() {
[Code]...
as the main class and a second class for the chips:
public class Spielstein {
public Spatial stone;
public int player;
public int team;
private AssetManager assetManager = Spielstein.getAM(); //THIS IS THE PROBLEM
public Spielstein(int t_player, int t_team){
[Code]...
My problem is: I can't access getAM() from the first in the second class. If you know why I would be glad for an answer.
View Replies
View Related
Mar 3, 2015
I’m trying to understand how to decide when to make a nested class static or non-static. These are my assumptions.
1) Make a nested class static if each instance of its enclosing class may have one or more instances of its nested class, for example, a HashMap has a static HashMap.Entry nested class because each HashMap instance may have one or more HashMap.Entry instances
2) Make a nested class non-static if each instance of its enclosing class must have only one instance of its nested class, for example, an AbstractButton has a non-static AbstractButton.Handler nested class because each AbstractButton instance must have only one AbstractButton.Handler instance.
View Replies
View Related
Sep 23, 2014
I wrote a code to use static class. But, when I call the class in a outer class but, it gives an error. Is it mandatory to have a static class should have static variables when we declaring them??
public class StaticClassMain {
static class Sub{
String str="Example 1";
}
public static void main(String[] args) {
System.out.println(Sub.str);
}
}
View Replies
View Related
Dec 8, 2014
I have a class Tree in which all the methods to build a tree are in place. But however I would want variable of by Tree which is pointing to the last node being added to the tree.
So basically every time you keep adding a node to the tree you tail pointer gets updated to point to the last node. I have this so far.
public class NonEmptyTree implements Tree {
private Tree left;
private int data;
private int leftleafCount;
private int rightleafCount;
private Tree right;
private Tree tail; // This variable must be shared by all the object. There needs to just one tail pointer to the tree.
public Tree insert( data ) {
tail = // gets updated every time when new node gets added.
View Replies
View Related
Mar 10, 2014
when do they get called?
from my code
class MyJava{
static { System.out.println("initializing..."); }
public static void main(String[] args)
{
}
}
i did get the "initializing..." string output, even though i didn't create any MyJava objects.
from this fact, i inferred that initializing blocks get called once when the program compiles.
am i right?
View Replies
View Related
Apr 14, 2015
Sandwich class. I have thus far completed creating a sandwich class with a seperate sandwich Tester class to run with it. (this is according to the assignment). Now I must create Static variables for the sandwich class:
Add two static variables to the Sandwich class to count how many sandwiches are sold and how many slices of tomato are used. Initialize each to 0.Where do you add code to increment the sandwich counter? Determine this and then add code.
public class Sandwich
{
static int numOfSold = 0;
static int slicesUsed = 0;
private String meat;
private int numOfSlicesOfTomato;
private boolean lettuce;
[code]....
View Replies
View Related
Apr 6, 2014
I am getting an error trying to access a static method of another class...theyre both in the same package, I've tried importing the class.
I've tried to do A b=new A()
and then
b.evaluate();
Everything that I try I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: B$A
Caused by: java.lang.ClassNotFoundException: B$A
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Code :
public class A{
public static String evaluate(String op) {
}
}
public class B{
String output=A.evaluate(input);
}
View Replies
View Related
Jun 22, 2014
is it necessary that inner classes inside a static method be static . If yes Why?
View Replies
View Related
Sep 11, 2014
A common solution to this problem is to write a utility class whose responsibility is to log information. This class can have a flag that will allow you to turn the logging on and off. In addition you should be able to tell the class how much detail you want in the output. Ultimately, this class will give you the ability to control when information is logged, what information is logged, how often information is logged, and even where the information is logged. And you would be able to control all of this without changing a single line of code!
This type of utility class is commonly written using static methods and is referred to as a static class. In order to use the features of a utility class, the application can access the methods directly by referring the class name, eliminating the need to create an instance of the class in order to execute the methods.
View Replies
View Related
Feb 2, 2015
Understanding the difference between static and dynamic class loading.
It will be more useful if examples are given , especially for dynamic class loading(without using reflection).
View Replies
View Related
Feb 9, 2015
While playing with arrays I've written this code:
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?
View Replies
View Related