Sponsored Content
Top Forums Programming Simplify setter and getter of java class Post 302990973 by colt on Friday 3rd of February 2017 04:14:32 PM
Old 02-03-2017
Personally, when passing values to a constructor, I would expect it to be to pass values that probably won't be changed until the object's end of life. When thinking of setters and getters, it's usually to be expected to change values that change a certain number of times. So in this case I wouldn't bother to create a constructor just to do something that the getter and setter do fine. Would create one just to pass value that I expect won't change until the object reaches the end of his life.

But there is nothing wrong with what you did, it's fine if you want to use it.
This User Gave Thanks to colt For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running java class with a cron

Hello everybody, I have a problem about running a java class with a cron : I have Cron.txt file which has : 0,5,10,15,20,25,30,35,40,45,50,55 * * * * CronJava.txt I have CronJava.txt wihich has : cd ias/j2ee/SapAktarim/applications/SapAktarim/SapAktarim/WEB-INF/classes/;java -classpath... (3 Replies)
Discussion started by: UBGandalf
3 Replies

2. Shell Programming and Scripting

Function loading in a shell scripting like class loading in java

Like class loader in java, can we make a function loader in shell script, for this can someone throw some light on how internally bash runs a shell script , what happenes in runtime ... thanks in advance.. (1 Reply)
Discussion started by: mpsc_sela
1 Replies

3. Shell Programming and Scripting

call constructor of java class in script

Hi, Is it possible to call the constructur of a java class in a shell script? I know you can call static methods, but can you also call the constructor? tnx. (1 Reply)
Discussion started by: thebladerunner
1 Replies

4. Fedora

Help, how to dynamicly load java class

Hi, everyone: I'm trying to connect to DB using JDBC on fedora. I have successfully installed jdk and it's ok to run common java program. The environment variables: JAVA_HOME=/installed/mycoy/jdk1.6.0 PATH=$JAVA_HOME/bin:$PATH... (3 Replies)
Discussion started by: mycoy
3 Replies

5. Programming

how abstract class differs in Java and C++?

hello all, i want to know if there is any difference in working and syntax declaration of abstract class in Java and C++. (1 Reply)
Discussion started by: haravivar
1 Replies

6. Programming

Help in JAVA main and class

Is anyone know how to write a class in separate file? While method does it needs to be contained in a printwriter class? Can I have the format of the printwriter class as a reference? Thanks a lot. (1 Reply)
Discussion started by: eel
1 Replies

7. Programming

Link array to class java

Hi, I need help to Link array from one class to another class Firstly CSVParser Class what it did is load csv file and store into array Secondly WarehouseItem where each record is store How can I get a list of array that I load to CSVParser Class and store them to WarehouseItem and... (0 Replies)
Discussion started by: guidely
0 Replies

8. Shell Programming and Scripting

setter and getter functions for file manipulation with sed

Hi, I would really appreciate some help, I couldn't nail my problem: I would like to create some setter and getter functions to make my life easier. my sample file contains: keyword - some tabs - value - semicolon number 12.1; float .3; double 12; real 12.2324; stuff .234; decimal... (5 Replies)
Discussion started by: Toorop
5 Replies

9. Programming

Java Date Class

I am looking at a website to learn Java and this is one of the exercises. Write a program that will show different time and date information based on what number you send it. The codes are: 0 - number of milliseconds since January 1, 1970 1 - number of seconds since January 1, 1970 2 -... (6 Replies)
Discussion started by: totoro125
6 Replies
Class::XSAccessor(3pm)					User Contributed Perl Documentation				    Class::XSAccessor(3pm)

NAME
Class::XSAccessor - Generate fast XS accessors without runtime compilation SYNOPSIS
package MyClass; use Class::XSAccessor replace => 1, # Replace existing methods (if any) constructor => 'new', getters => { get_foo => 'foo', # 'foo' is the hash key to access get_bar => 'bar', }, setters => { set_foo => 'foo', set_bar => 'bar', }, accessors => { foo => 'foo', bar => 'bar', }, predicates => { has_foo => 'foo', has_bar => 'bar', }, lvalue_accessors => { # see below baz => 'baz', # ... }, true => [ 'is_token', 'is_whitespace' ], false => [ 'significant' ]; # The imported methods are implemented in fast XS. # normal class code here. As of version 1.05, some alternative syntax forms are available: package MyClass; # Options can be passed as a HASH reference, if preferred, # which can also help Perl::Tidy to format the statement correctly. use Class::XSAccessor { # If the name => key values are always identical, # the following shorthand can be used. accessors => [ 'foo', 'bar' ], }; DESCRIPTION
Class::XSAccessor implements fast read, write and read/write accessors in XS. Additionally, it can provide predicates such as "has_foo()" for testing whether the attribute "foo" is defined in the object. It only works with objects that are implemented as ordinary hashes. Class::XSAccessor::Array implements the same interface for objects that use arrays for their internal representation. Since version 0.10, the module can also generate simple constructors (implemented in XS). Simply supply the "constructor => 'constructor_name'" option or the "constructors => ['new', 'create', 'spawn']" option. These constructors do the equivalent of the following Perl code: sub new { my $class = shift; return bless { @_ }, ref($class)||$class; } That means they can be called on objects and classes but will not clone objects entirely. Parameters to "new()" are added to the object. The XS accessor methods are between 3 and 4 times faster than typical pure-Perl accessors in some simple benchmarking. The lower factor applies to the potentially slightly obscure "sub set_foo_pp {$_[0]->{foo} = $_[1]}", so if you usually write clear code, a factor of 3.5 speed-up is a good estimate. If in doubt, do your own benchmarking! The method names may be fully qualified. The example in the synopsis could have been written as "MyClass::get_foo" instead of "get_foo". This way, methods can be installed in classes other than the current class. See also: the "class" option below. By default, the setters return the new value that was set, and the accessors (mutators) do the same. This behaviour can be changed with the "chained" option - see below. The predicates return a boolean. Since version 1.01, "Class::XSAccessor" can generate extremely simple methods which just return true or false (and always do so). If that seems like a really superfluous thing to you, then consider a large class hierarchy with interfaces such as PPI. These methods are provided by the "true" and "false" options - see the synopsis. OPTIONS
In addition to specifying the types and names of accessors, additional options can be supplied which modify behaviour. The options are specified as key/value pairs in the same manner as the accessor declaration. For example: use Class::XSAccessor getters => { get_foo => 'foo', }, replace => 1; The list of available options is: replace Set this to a true value to prevent "Class::XSAccessor" from complaining about replacing existing subroutines. chained Set this to a true value to change the return value of setters and mutators (when called with an argument). If "chained" is enabled, the setters and accessors/mutators will return the object. Mutators called without an argument still return the value of the associated attribute. As with the other options, "chained" affects all methods generated in the same "use Class::XSAccessor ..." statement. class By default, the accessors are generated in the calling class. The the "class" option allows the target class to be specified. LVALUES
Support for lvalue accessors via the keyword "lvalue_accessors" was added in version 1.08. At this point, THEY ARE CONSIDERED HIGHLY EXPERIMENTAL. Furthermore, their performance hasn't been benchmarked yet. The following example demonstrates an lvalue accessor: package Address; use Class::XSAccessor constructor => 'new', lvalue_accessors => { zip_code => 'zip' }; package main; my $address = Address->new(zip => 2); print $address->zip_code, " "; # prints 2 $address->zip_code = 76135; # <--- This is it! print $address->zip_code, " "; # prints 76135 CAVEATS
Probably won't work for objects based on tied hashes. But that's a strange thing to do anyway. Scary code exploiting strange XS features. If you think writing an accessor in XS should be a laughably simple exercise, then please contemplate how you could instantiate a new XS accessor for a new hash key that's only known at run-time. Note that compiling C code at run-time a la Inline::C is a no go. Threading. With version 1.00, a memory leak has been fixed. Previously, a small amount of memory would leak if "Class::XSAccessor"-based classes were loaded in a subthread without having been loaded in the "main" thread. If the subthread then terminated, a hash key and an int per associated method used to be lost. Note that this mattered only if classes were only loaded in a sort of throw-away thread. In the new implementation, as of 1.00, the memory will still not be released, in the same situation, but it will be recycled when the same class, or a similar class, is loaded again in any thread. SEE ALSO
o Class::XSAccessor::Array o AutoXS AUTHOR
Steffen Mueller <smueller@cpan.org> chocolateboy <chocolate@cpan.org> COPYRIGHT AND LICENSE
Copyright (C) 2008, 2009, 2010, 2011 by Steffen Mueller This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8 or, at your option, any later version of Perl 5 you may have available. perl v5.14.2 2011-12-12 Class::XSAccessor(3pm)
All times are GMT -4. The time now is 10:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy