class DuckPrivate
{
private static int size;
public static void main(String [] args)
{
Duck d=new Duck();
d.setSize(25);
d.getSize();
System.out.println("size of duck is "+size);
}
public static void setSize(int s)
{
size=s;
}
public static int getSize()
{
return size;
}
}
I am getting the error cannot find the symbol getSize(),why i am getting this error,is it possible to print the static method.
Duck d = new Duck(); you are calling set/getSize method of Duck not DuckPrivate and note d.getSize(); if exist will return the value which you need to store.In DuckPrivate you can simply call setSize(25); as it's static and just print size.
Related
I am just making a random class so that i can mess around and learn java with it. Ive kinda got this concept of arguments down.
public class OffensiveLine {
public static void main(String args[]){
String[] blocks = {"Swim Move", "Hello"};
LineMan jeff = new LineMan(80, 90);
int i = 1;
WideReciever.block(32, blocks[i]);
jeff.block();
}
}
public class WideReciever extends Speed{
static Date now = new Date(1);
public WideReciever() {
// TODO Auto-generated constructor stub
super();
}
public static void run(){
}
public static void block(int b, String[] wow){
int i = 1;
System.out.println(wow[i]);
}
}
public static void block(int b, String[] wow){
This method wants a String array.
However, you call it like this:
WideReciever.block(32, blocks[i]);
blocks is a String array, but blocks[i] is just a string.
You could either change the block method to take a string, or pass the array in:
public static void block(int b, String[] wow) {
System.out.println(wow[1]);
}
or:
WideReciever.block(32, blocks);
This
WideReciever.block(32, blocks[i]);
Is passing in an int and a String to a method which accepts an int and a String array. What you are likely trying to do is:
WideReciever.block(32, blocks);
I am fairly new to java, and am having what I assume is a simple problem with my program.
For the method arrayTest2, I cannot import it into main due to an error on compilation:
"Cannot find symbol, symbol: variable dataStorage".
I have tried also tried the declarations:
arrayTest2(dataStorage[][])
and
arrayTest2(dataStorage[5][5])`
but they don't work.
Any help would be greatly appreciated.
Cheers
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
The problem here is the scope: Something defined in one function is not visible in another. What you will normally do to solve this is to return the value. Something like this:
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
int[][] dataStorage = arrayTest();
arrayTest2(dataStorage);
}
public static int[][] arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
return dataStorage;
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
Alternatively you could have your dataStorage field as a global variable, this is however potentially very confusing. To do that you'd define
public class TrialArray
{
private static int[][] dataStorage;
// ...
public static void arrayTest() {
dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
// ...
}
on this line
arrayTest2(dataStorage);
You are passing parameter to method, that has one argument "dataStorage", but you don't declare it.
You try to pass dataStorage to your arrayTest-function, but dataStorage is not a field of the class, neither is it a local variable of main (aka dataStorage does not exist in main).
public static void main(String [] args) {
arrayTest();
arrayTest2(dataStorage); //<------- What is dataStorage?
}
Here is a little tutorial on variable scopes in Java. You probably want to return the array you created in arrayTest() and use it, but I am just guessing what you want to do.
You cant access variables in declared inside other methods.
To make it work, you would have to do this:
public class TrialArray
{
int[][] dataStorage;
public static void main(String [] args)
{
dataStorage = new int[5][5];
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
I wanted to create a static method which prints the contents of an array.I wrote one for String[] as below
public static void print(String[] a){
for(String x : a){
System.out.print(x+", ");
}
System.out.println();
}
I thought I could create a method which takes in a generic type ,and modified the code as below
public class ArrayPrinting<E> {
public static void printArray(E[] a){
for(E x : a){
System.out.print(x+", ");
}
System.out.println();
}
public static void main(String[] args) {
String[] a = {"A","B","C","D","E"};
}
}
But,this gives a compiler error
'Cannot make a static reference to the non-static type E'
So,how do I create such a method?or is it impossible ? Since this is a static method, I wonder how I can invoke the method without creating an instance. A call like
ArrayPrinting<E>.printArray(a) doesn't look right ..
Can someone help?
Try this
public class ArrayPrinting {
public static <E> void printArray(E[] a){
for(E x : a){
System.out.print(x+", ");
}
System.out.println();
}
public static void main(String[] args) {
String[] a = {"A","B","C","D","E"};
ArrayPrinting.printArray(a);
}
}
Ravi already covered the proper syntax for a generic method. I just want to point out that this particular method doesn't need to be generic:
public static void printArray(Object[] a) {
for (Object x : a) {
System.out.print(x + ", ");
}
System.out.println();
}
The reason this works is array covariance - a String[] is an Object[].
Class's generic type parameters are for class level variables and methods (instance variables and methods).So you can't use it.
You can handle it by declaring type parameter in the method itself:
public static <E> void printArray(E[] a){
.............
}
public class ArrayPrinting<E> {
public void printArray(E[] a){
for(E x : a){
System.out.print(x+", ");
}
System.out.println();
}
public static void main(String[] args) {
String[] a = {"A","B","C","D","E"};
new ArrayPrinting().printArray(a);
}
}
Can i use the following code? It's not throwing any error at the Object but at obj.i. Is this a legal way of using an object? Also, how many ways can i create an object other than using the normal syntax obj s = new obj();
public class Test {
static int i;
static Test obj;
obj.i = 10; //am getting a compilation error here "Syntax error on token "i", VariableDeclaratorId expected after this token"
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
You need to place a static block around the obj.i assignment statement for this to work:
public class Test {
static int i;
static Test obj;
static { obj.i = 10; }
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
This is not, you didn't initialize. Furthermore you might not want to use static.
public static void main(String [] args) {
int i = 10;
Test obj = new Test();
obj.setI(i);
System.out.println("my objects I = "+ obj.getI());
}
now in your Test object
public class Test {
private int i;
public void setI(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}
Is it possible to do something like this using Java features?
class myClass {int i; String s;}
static void myMethod(myClass... args)
{
...
}
main()
{
myMethod(2,"two",3,"three");
}
It is not possible. Perhaps you could create a static helper method which makes creating your objects as easy as possible.
static myClass mc(int i, String s) {
return new myClass(i, s);
}
myMethod(mc(2, "two"), mc(3, "three"));
No, but you can make a constructor of MyClass and invoke:
myMethod(new MyClass(2, "two"), new Myclass(3, "three"));
For the sake of brevity you can make a statically-imported factory method:
public class MyClass {
public MyClass create(String s, int i) {
return new MyClass(s, i);
}
}
and use:
myMethod(create(2, "two"), create(3, "three"));
You could do it with a wrapper class:
class MyWrapper {
private int i;
private String s;
public MyWrapper(int _i, String _s) {
i = _i;
s = _s;
}
}
class Test {
static void myMethod(MyWrapper... args) {
//do work
}
public static void main(String[] args) {
myMethod(new MyWrapper(2, "two"), new MyWrapper(3, "three"));
}
}
Yes, sortof. You need a constructor of your class...
public class MyClass{
int i
String s;
public MyClass(int i, String s){
this.i = i;
this.s = s;
}
}
public static void myMethod(MyClass... instances){
.....
}
public static void myMethod(Object... args){
MyClass[] instances = new MyClass[args.length / 2];
for (int i=0; i<args.length / 2; i++){
instances[i] = new MyClass((Integer)args[i * 2], (String)args[(i*2) + 1]);
}
myMethod(instances);
}
You would need to add error checking to ensure that args has an even number of elements and there is not a method to enforce that every i * 2 element is an Integer and every i * 2 + 1 is a String. But it is possible.
Given all of the above, I will say... this is very non-standard programming and I would not recommend it. But as you see, it is possible.