Java: concerning nested classes - java

I have a few classes I need: Score, Course, and handicap. The file is "handicap.java", thus the main class is "handicap".
If I try and nest the Score class or the Course class inside of the "handicap" class, I receive this error upon trying to instantiate an instance of either class:
handicap.java:129: non-static variable this cannot be referenced from a static context
Score sc = new Score(score, course);
^
handicap.java:141: put(java.util.GregorianCalendar,Score) in java.util.Map<java.util.GregorianCalendar,Score> cannot be applied to (java.util.GregorianCalendar,handicap.Score)
g.scores.put(greg, sc);
If I add "static" to the Score class declaration, I still receive the second error. Help?
The Code is here: http://pastebin.com/CvT1SCvb

take a look on this
public class Handicap {
public class Score{
}
public static class ScoreStatic{
}
public static void main(String[] args) {
Handicap h = new Handicap();
h.method();
new Handicap.ScoreStatic();
}
public void method(){
new Handicap.Score();
}
}

By the looks of it , you are getting compilation error.
Please read about the Nested Classes : http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
This is the way it works :
public class handicap{
public static void main(String... args) {
handicap st = new handicap();
handicap.Score fl = st.new Score();
}
class Score{
//blah
}
}

Related

How does this basic hello world statement come as an error?

class helloworld{
public static void main(String[] args){
speaker speakerObject = new speaker(); //this becomes an error!!!
speakerObject.greet();
}
public class speaker {
public void greet(){
System.out.println("Hello World!");
}
}
}
when i try to make an object, it becomes an error!
im using VS
You're referencing a non-static variable in a static context, and you have an error in the inner class declaration, basically it should be:
public void main(String[] args){
speaker speakerObject = new speaker();
speakerObject.greet();
}
class speaker {
public void greet(){
System.out.println("Hello World!");
}
}
It's an error of No enclosing instance of type __YourClassName__ is accessible.
Try to understand the error, speaker is an inner class of helloworld, which is by definition is associated with helloworld class. So declaring it as a static class, it won't need any instance of helloworld class.
And one more thing, If you are new to java, cultivate a habit of using first letter capital in your class name, It will be strongly appreciated if you use upper camel case for naming conventions of class.
Below is the corrected code:
class helloworld{
public static void main(String[] args){
speaker speakerObject = new speaker(); //this becomes an error!!!
speakerObject.greet();
}
public static class speaker {
public void greet(){
System.out.println("Hello World!");
}
}
}

Accessing objects of other classes java

I want to access an object i created in a new class but it returns that the object " cannot be resolved".
thanks for anyone who helps:)
here is my code :
public class lion {
int weight;
int height;
String color;
double roardecibles;
public void lioncolor() {
System.out.println(color);
}
}
public class blacklion {
lion blackLion;{
blackLion = new lion();
blackLion.weight =4;
blackLion.height =3;
blackLion.color = "black";
blackLion.roardecibles = 5.5;
}
}
public class zoo {
public static void main(String[] args) {
blackLion.lioncolor(); //here it dosent work//
}
}
There's a difference between an object and a class. Think of a class as a blueprint, that's what you did when you defined it in public class blacklion. But you didn't actually build something with the blueprint. To create an object you have to instantiate it, using the new keyword.
public static void main(String[] args) {
blacklion lion = new blacklion();
lion.lioncolor(); //here it dosent work//
}
Usually, you want to instantiate an Object of your class that you can then access from where u created it. If you want to access methods or variables of a class directly you want to declare those as public and static.
Hey looks like you should check out this resource:
https://docs.oracle.com/javase/tutorial/
Specifically the section of how classes work:
https://docs.oracle.com/javase/tutorial/java/concepts/class.html
this code
public static void main(String[] args)
{
blackLion.lioncolor(); //here it dosent work//
}
needs an instance of the object in order to call the lioncolor() method

How do I create a proper Java instance?

I have created a public class helloworld and I am trying to create an object for class Abc. I have used "new" keyword to create an instance for Abc class.But still I am getting an error "non-static variable this cannot be referenced from a static context." in 4th line.
How do I solve this problem?
public class helloworld {
public static void main (String[] args)
{
Abc obj = new Abc();
}
class Abc
{
int i;
}
}
For simplicity, put class Abc in a different file and make it a public class.
helloworld.java
public class helloworld {
public static void main (String[] args)
{
Abc obj = new Abc();
}
}
Abc.java
public class Abc
{
int i;
}
Or you can declare your class Abc as static:
public class helloworld {
public static void main (String[] args)
{
Abc obj = new Abc();
}
static class Abc
{
int i;
}
}
When you create an inner class, it follows the same rule as members of a class: Static members of a class cannot directly access Instance members. Thus, you will need to declare Abc as a static class.
PS: please use CamelCasing conventions to name your classes. Ex: Use HelloWorld instead of helloworld.
Hope this helps!
First of all: Heed Jon's advise. That said, you can quickly solve this particular problem by changing your inner class into a static class:
static class Abc {
int i;
}
I know this might seem a bit weird, but you cannot refer to regular inner classes without first creating an instance of the outer class. Since the main method is static there is no instance of HelloWorld yet. This means it cannot refer to a regular (instance-bound) inner class only to a static inner class.
Arguably a better solution is to create an instance of your class first thing in your main method:
// class names should be camel case with an upper case first letter
public class HelloWorld {
public static void main (String[] args) {
HelloWorld app = new HelloWorld();
app.start();
}
// note that this is not a static method
private void start() {
Abc obj = new Abc();
}
private class Abc {
int i;
}
}
Abc is an inner class of helloworld. To instantiate it you need an instance of helloworld
Abc abc = new helloworld().new Abc();
or make Abc a static inner class.
static class Abc {}
The first awnser is correct but don't be easy. This is the easy way for a eclipse project o similar.
Create a ABC class:
public class Abc
{
int i;
}
Import in a Hello world class and declare static:
import Abc;
public class HelloWorld{
private static Abc abc=new Abc();
public static void main(String[] args){
//do something
}
}
All IDE that you use. Help you to import the class : )

java - can a class type parameter from a static main class be passed to another class

I have 2 classes the static main class and class B. I'm trying to pass main to B, where there is a method that sets fields.
Can this be done?
If so, could you please provide examples?
public static void main(String[] args) {
ArrayList a = new ArrayList()
class b = new class()
b.update(b);
}
class a {
public void update(ArrayList a) {
//updates the encapsulated arrayList field
}
}
The error message keeps on saying that one is static and the other is non-static, but they should be pointing the same object
I'm not entirely sure what you are trying to do, but here is an example that shows that you can pass an instance of the main class into another class:
public class A {
private String str = null;
public static void main(String[] args) {
A a = new A();
B b = new B(a);
System.out.println(a.getStr());
}
public String getStr() {
return this.str;
}
public void setStr(String str) {
this.str = str;
}
}
public class B {
public B(A a) {
a.setA("hello");
}
}
Running this code will print out hello.
main is static and public, so you can call it from any other class as any other public static method: statically.
if you have a class A that contains a
public static void main(String[] args)
method, then class B can call this method like
A.main(s);
where s is String[]
your question is far from clear, so I suggest you to add more code samples to make it clear what you're really trying to do.

Java calling object in method outside of main

I have a simple problem that I've been stuck on for some time and I can't quite find the answer to. Basically, I'm creating an object and trying to access the variables without using static variables as I was told that is the wrong way to do it. Here is some example code of the problem. I receive an error in the first class that can not be resolved to a variable. What I would like to be able to do is access t.name in other methods outside of the main, but also in other classes as well. To get around this previously I would use Test2.name and make the variable static in the Test2 class, and correct me if I'm wrong but I believe that's the wrong way to do it. Any help would be greatly appreciated =)
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
public class Test2 {
String name;
public Test2 (String nm) {
name = nm;
}
}
I see others have posted code snippets, but they haven't actually posted why any of this works (at the time of this writing.)
The reason you are getting a compilation error, is that in your method
public static void main(String[] args) {
Test2 t = new Test2("Joe");
}
Variable t's scope is just that method. You are defining Test2 t to only be in the main(String[] args) method, so you can only use the variable t in that method. However, if you were to make the variable a field, like so, and create a new instance of the Test class,
public class Test {
Test2 t;
public static void main(String[] args) {
Test test = new Test();
test.t = new Test2("Joe");
test.displayName();
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
Then you should no longer be getting any compilation errors, since you are declaring the variable t to be in the class Test scope.
You may give the reference to your test object as an argument to method displayName:
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
displayName(t);
}
public static void displayName(Test2 test) {
System.out.println("Name2: " + test.name);
}
}
Note: I also made displayName a static method. From within your main method you can only access static methods without reference.
Modify the Test class to this
public class Test {
private static Test2 t;
public static void main(String[] args) {
t = new Test2("Joe");
}
public void displayName() {
System.out.println("Name2: " + t.name);
}
}
Use a getter for your purpose. This is a side solution to your problem, but generally this is how you should retrieve instance variables, using getters.
public class Test {
public static void main(String[] args) {
Test2 t = new Test2("Joe");
displayName(t);
}
public static void displayName(Test2 test) {
System.out.println(test.getName());
}
}
public class Test2
{
private String name;
public Test2 (String nm)
{
name = nm;
}
public String getName()
{
return name;
}
}
Always remember, variables in your class should be private. That protects it from access from outside the class. Hence, getters are the only way to access them. And setters or constructors to initialize them.
Fewer statics the better I reckon.
I would Instantiate Test and call displayName on the instance of it, then pass the instance of Test2 to displayName.
But it does depend on what the overall aim is

Categories